Skip to content

Latest Release Code Demo using JavaScript / Typescript-sdk. A comprehensive demonstration of Temporal's Priority Scheduling and Fairness features using Node.js, Express, and React. This project showcases how Temporal manages workflow execution priorities and ensures fair resource distribution across different workload classes.

Notifications You must be signed in to change notification settings

shyamg090/temporal-priority-fairness-demo-javascript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Temporal Priority & Fairness Demonstration

A comprehensive demonstration of Temporal's Priority Scheduling and Fairness features using Node.js, Express, and React. This project showcases how Temporal manages workflow execution priorities and ensures fair resource distribution across different workload classes.


๐Ÿ“‹ Table of Contents


๐ŸŽฏ Overview

This project demonstrates two key Temporal scheduling capabilities:

๐Ÿ”ข Priority Mode

Workflows are distributed across 5 priority levels (1-5, where 1 is highest priority). Temporal schedules higher-priority workflows first, ensuring critical tasks complete before lower-priority ones.

Use Case: Emergency orders (P1) should be processed before standard orders (P3-P5).

โš–๏ธ Fairness Mode

Multiple tenant classes share worker capacity fairly based on weight ratios. Prevents resource starvation where one tenant monopolizes all workers.

Use Case: VIP customers (weight 20) receive more capacity than economy customers (weight 1), but economy still gets guaranteed processing.


๐Ÿ— Architecture

System Components

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                        CLIENT LAYER                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚  React UI    โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค  Express API Server          โ”‚     โ”‚
โ”‚  โ”‚  (Port 5173) โ”‚         โ”‚  (Port 3001)                 โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜         โ”‚  - REST endpoints            โ”‚     โ”‚
โ”‚                           โ”‚  - Temporal client           โ”‚     โ”‚
โ”‚                           โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                         โ”‚
                                         โ”‚ gRPC
                                         โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    TEMPORAL SERVER                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”‚
โ”‚  โ”‚              Temporal Core Engine                     โ”‚      โ”‚
โ”‚  โ”‚  - Workflow Scheduler                                โ”‚      โ”‚
โ”‚  โ”‚  - Priority/Fairness Queue Manager                   โ”‚      โ”‚
โ”‚  โ”‚  - State Machine Manager                             โ”‚      โ”‚
โ”‚  โ”‚  - History Service                                   โ”‚      โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ”‚
โ”‚                        โ”‚                                        โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”‚
โ”‚  โ”‚         PostgreSQL Database                          โ”‚      โ”‚
โ”‚  โ”‚  - Workflow execution history                        โ”‚      โ”‚
โ”‚  โ”‚  - Search attributes                                 โ”‚      โ”‚
โ”‚  โ”‚  - Task queue state                                  โ”‚      โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ”‚
โ”‚                                                                 โ”‚
โ”‚  Temporal UI (Port 8080) - Monitoring & Debugging              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                     โ”‚
                                     โ”‚ gRPC Long Poll
                                     โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                      WORKER LAYER                               โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”‚
โ”‚  โ”‚           Temporal Worker Process                     โ”‚      โ”‚
โ”‚  โ”‚                                                       โ”‚      โ”‚
โ”‚  โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”‚      โ”‚
โ”‚  โ”‚  โ”‚ Priority Worker  โ”‚    โ”‚ Fairness Worker  โ”‚       โ”‚      โ”‚
โ”‚  โ”‚  โ”‚ Queue: priority- โ”‚    โ”‚ Queue: fairness- โ”‚       โ”‚      โ”‚
โ”‚  โ”‚  โ”‚       queue      โ”‚    โ”‚       queue      โ”‚       โ”‚      โ”‚
โ”‚  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜       โ”‚      โ”‚
โ”‚  โ”‚           โ”‚                       โ”‚                  โ”‚      โ”‚
โ”‚  โ”‚           โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค                  โ”‚      โ”‚
โ”‚  โ”‚           โ”‚                                          โ”‚      โ”‚
โ”‚  โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”‚      โ”‚
โ”‚  โ”‚  โ”‚      Workflow Implementations             โ”‚      โ”‚      โ”‚
โ”‚  โ”‚  โ”‚  - priorityWorkflow()                     โ”‚      โ”‚      โ”‚
โ”‚  โ”‚  โ”‚  - fairnessWorkflow()                     โ”‚      โ”‚      โ”‚
โ”‚  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ”‚      โ”‚
โ”‚  โ”‚           โ”‚                                          โ”‚      โ”‚
โ”‚  โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”‚      โ”‚
โ”‚  โ”‚  โ”‚      Activity Implementations             โ”‚      โ”‚      โ”‚
โ”‚  โ”‚  โ”‚  - runPriorityActivity()                  โ”‚      โ”‚      โ”‚
โ”‚  โ”‚  โ”‚  - runFairnessActivity()                  โ”‚      โ”‚      โ”‚
โ”‚  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ”‚      โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ”ง How Temporal Works

Workflow Execution Lifecycle

1. CLIENT STARTS WORKFLOW
   โ”œโ”€ API sends gRPC request to Temporal Server
   โ”œโ”€ Server creates workflow execution record in DB
   โ”œโ”€ Server adds workflow task to task queue
   โ””โ”€ Returns workflow ID to client

2. WORKER POLLS FOR TASKS
   โ”œโ”€ Worker sends gRPC long poll request
   โ”œโ”€ Server matches task to worker based on priority/fairness
   โ””โ”€ Server returns workflow task to worker

3. WORKER EXECUTES WORKFLOW
   โ”œโ”€ Worker runs workflow code (deterministic)
   โ”œโ”€ Workflow schedules activities
   โ”œโ”€ Worker reports events back to server via gRPC
   โ””โ”€ Server persists events to history

4. ACTIVITY EXECUTION
   โ”œโ”€ Server adds activity task to queue
   โ”œโ”€ Worker polls and receives activity task
   โ”œโ”€ Worker executes activity code (can be non-deterministic)
   โ”œโ”€ Worker reports activity result via gRPC
   โ””โ”€ Server updates workflow state

5. WORKFLOW COMPLETION
   โ”œโ”€ Workflow completes after all activities
   โ”œโ”€ Worker reports completion to server
   โ”œโ”€ Server marks workflow as completed
   โ””โ”€ Client can query final state

โš™๏ธ Terminal 2 โ€“ Backend Setup

cd server

# Install dependencies
npm install

# Register search attributes (one-time setup)
chmod +x setup-search-attributes.sh
./setup-search-attributes.sh

# Start API server
npm run dev
# API Server: http://localhost:3001

๐Ÿงต Terminal 3 โ€“ Start Worker

cd server

# Start Temporal worker
npm run worker
# Worker polls from priority-queue and fairness-queue

๐ŸŽจ Optional: Start React UI

cd ui
npm install
npm run dev
# UI: http://localhost:5173

โœจ Features

Priority Mode

  • โœ… Distributes workflows across 5 priority levels (1=highest, 5=lowest)
  • โœ… Higher priority workflows complete first
  • โœ… Visualize completion rates by priority level
  • โœ… Real-time progress tracking via search attributes

Fairness Mode

  • โœ… Multiple tenant classes with configurable weights
  • โœ… Fair capacity distribution (prevents starvation)
  • โœ… Pre-configured scenarios (airline classes, service tiers)
  • โœ… Compare fairness ON vs OFF

General Features

  • โœ… React UI with Material-UI components
  • โœ… Real-time workflow monitoring
  • โœ… Historical results visualization
  • โœ… RESTful API for workflow management
  • โœ… Docker Compose for easy setup

๐Ÿ“ก API Documentation

POST /start-workflows

Start a batch of workflows in priority or fairness mode.

Request Body (Priority Mode):

{
  "workflowIdPrefix": "priority-test-1",
  "numberOfWorkflows": 100,
  "mode": "priority"
}

Request Body (Fairness Mode):

{
  "workflowIdPrefix": "fairness-test-1",
  "numberOfWorkflows": 220,
  "mode": "fairness",
  "disableFairness": false,
  "bands": [
    { "key": "vip", "weight": 20, "count": 10 },
    { "key": "first-class", "weight": 10, "count": 20 },
    { "key": "business-class", "weight": 5, "count": 40 },
    { "key": "economy-class", "weight": 2, "count": 75 },
    { "key": "standby-list", "weight": 1, "count": 75 }
  ]
}

GET /run-status?runPrefix=

Get priority mode test results.

Response:

{
  "totalWorkflows": 100,
  "completedWorkflows": 85,
  "priorityDistribution": {
    "1": { "total": 20, "completed": 20, "rate": 1.0 },
    "2": { "total": 20, "completed": 19, "rate": 0.95 },
    "3": { "total": 20, "completed": 18, "rate": 0.90 },
    "4": { "total": 20, "completed": 16, "rate": 0.80 },
    "5": { "total": 20, "completed": 12, "rate": 0.60 }
  }
}

GET /run-status-fairness?runPrefix=

Get fairness mode test results.

Response:

{
  "totalWorkflows": 220,
  "fairnessBands": {
    "vip": { "count": 10, "completed": 9, "avgActivities": 4.5 },
    "first-class": { "count": 20, "completed": 18, "avgActivities": 4.2 },
    "business-class": { "count": 40, "completed": 35, "avgActivities": 3.8 }
  }
}

๐ŸŽฎ Usage Examples

Example 1: Priority Test via API

# Start 100 workflows with priority scheduling
curl -X POST http://localhost:3001/start-workflows \
  -H "Content-Type: application/json" \
  -d '{
    "workflowIdPrefix": "test-241124",
    "numberOfWorkflows": 100,
    "mode": "priority"
  }'

# Wait 30 seconds, then check results
curl "http://localhost:3001/run-status?runPrefix=test-241124"

Example 2: Fairness Test via UI

  1. Open http://localhost:5173
  2. Select "Fairness Mode"
  3. Choose "Airline Classes" preset
  4. Click "Start Workflow Test"
  5. View results in real-time

Example 3: Monitor via Temporal UI

  1. Open http://localhost:8080
  2. Navigate to Workflows
  3. Filter by workflow ID prefix
  4. View execution history and search attributes

๐Ÿ“š Additional Resources


๐Ÿค Contributing

This is a demonstration project. Feel free to fork and experiment!


๐Ÿ“„ License

ISC

About

Latest Release Code Demo using JavaScript / Typescript-sdk. A comprehensive demonstration of Temporal's Priority Scheduling and Fairness features using Node.js, Express, and React. This project showcases how Temporal manages workflow execution priorities and ensures fair resource distribution across different workload classes.

Topics

Resources

Stars

Watchers

Forks