Skip to main content
Temporal Server persists workflow state, history, and task queues in a database. This guide covers persistence layer operations and optimization.

Persistence Architecture

Temporal uses two types of data stores:
  1. Default Store - Core workflow data, task queues, and system state
  2. Visibility Store - Workflow search and list operations

Data Store Types

Supported databases:
  • Cassandra - Horizontally scalable, high throughput
  • PostgreSQL - ACID compliant, strong consistency
  • MySQL - ACID compliant, widespread support
  • SQLite - Development and testing only

Configuration

Basic Setup

History Shards

Workflow executions are sharded across multiple partitions:
Shard Selection:
  • Based on workflow ID hash
  • Immutable after cluster creation
  • Higher count = better parallelism
Recommended Shard Counts:
  • Development: 1-4
  • Small production: 128-512
  • Medium production: 1024-2048
  • Large production: 4096-16384

Cassandra Configuration

Connection Settings

Connection Pool:
  • maxConns - Maximum connections per host (default: 2)
  • Recommended: 10-20 for high throughput
  • Total connections = maxConns × number of hosts × number of history nodes

Consistency Configuration

Consistency Levels:
  • LOCAL_QUORUM - Majority of replicas in local datacenter (recommended)
  • QUORUM - Majority across all datacenters
  • ONE - Single replica (not recommended)

TLS Configuration

Address Translation

For environments where Cassandra returns non-routable IPs:

Cassandra Best Practices

  1. Replication Factor: 3 minimum for production
  2. Compaction Strategy: LeveledCompactionStrategy for temporal tables
  3. Read Repair: Disabled for better performance
  4. Monitoring: Track read/write latency, compaction lag
  5. Separate Clusters: Use different clusters for default and visibility

SQL Configuration (PostgreSQL/MySQL)

PostgreSQL

MySQL

Connection Pool Tuning

Pool Sizing:
For 4096 shards across 16 history nodes:

SQL TLS Configuration

Vitess (MySQL Sharding)

For large-scale MySQL deployments:

Visibility Store Configuration

Elasticsearch

Index Sharding:
  • Start with 5 primary shards
  • Increase to 10-20 for > 100M workflows
  • Use 1-2 replicas for production

Dual Visibility

Run two visibility stores simultaneously:
Useful for:
  • Migration from one visibility store to another
  • Comparing query results
  • Fallback during maintenance

Schema Management

Initial Setup

Temporal provides schema files in /schema directory:

Schema Updates

Upgrade to newer Temporal versions:

Schema Versioning

Temporal tracks schema version in the database:
  • /schema/cassandra/temporal/versioned/ - Cassandra schemas
  • /schema/postgresql/v12/temporal/versioned/ - PostgreSQL schemas
  • /schema/mysql/v8/temporal/versioned/ - MySQL schemas

Persistence Metrics

Operation Metrics

All persistence operations emit metrics:
Each emits:
  • Request count
  • Error count
  • Latency histogram
  • Tagged with db_kind

Monitoring Query

Critical Metrics

  1. Shard Operations
    • GetOrCreateShard - Should be fast (< 10ms)
    • UpdateShard - Latency impacts failover
  2. Workflow Operations
    • UpdateWorkflowExecution - Most frequent, optimize heavily
    • CreateWorkflowExecution - Directly affects start rate
  3. Task Operations
    • GetTransferTasks - Affects task dispatch latency
    • GetTimerTasks - Affects timer firing accuracy

Data Retention

Workflow Retention

Set retention per namespace:
Or update existing:
Retention Behavior:
  • Applies to closed workflows only
  • History deleted after retention period
  • Visibility records removed
  • Does not affect running workflows

Database Cleanup

Cassandra:
  • Uses TTL on history tables
  • Automatic compaction removes expired data
  • No manual cleanup needed
SQL Databases:
  • History scavenger deletes old records
  • Runs as system workflow
  • Configure via dynamic config:

Backup and Recovery

Cassandra Backup

PostgreSQL Backup

MySQL Backup

Recovery Considerations

  1. Consistency: Backup all datastores simultaneously
  2. Downtime: Stop Temporal services during restore
  3. Testing: Regularly test restore procedures
  4. Point-in-Time: Use transaction logs for precise recovery

Troubleshooting

High Latency

Symptoms:
  • Persistence metrics show high p99 latency
  • Workflow operations slow
Solutions:
  1. Check database server metrics (CPU, I/O)
  2. Review query execution plans
  3. Verify connection pool not exhausted
  4. Check network latency to database
  5. Add read replicas (not recommended for writes)

Connection Pool Exhaustion

Symptoms:
  • connection refused errors
  • too many connections errors
Solutions:
  1. Increase maxConns in config
  2. Add more history nodes to distribute load
  3. Increase database connection limits
  4. Check for connection leaks

Data Inconsistency

Symptoms:
  • Workflow state doesn’t match expected
  • Missing history events
Solutions:
  1. Verify consistency settings (Cassandra)
  2. Check for split-brain scenarios
  3. Review replication lag
  4. Verify no partial failures during writes

Schema Version Mismatch

Symptoms:
  • schema version mismatch errors
  • Server fails to start
Solutions:
  1. Check schema version: SELECT * FROM schema_version;
  2. Run schema update tool
  3. Ensure all nodes use same version
  4. Review schema update logs

Performance Optimization

Cassandra

  1. Compaction: Use LeveledCompactionStrategy
  2. Caching: Enable row cache for small workflows
  3. GC: Tune JVM for low pause times
  4. Replication: Use LOCAL_QUORUM for better performance

PostgreSQL

  1. Indexes: Ensure all indexes are healthy
  2. VACUUM: Run auto-vacuum regularly
  3. Shared Buffers: Set to 25% of RAM
  4. Work Memory: Increase for large queries
  5. Connection Pooling: Use pgBouncer

MySQL

  1. InnoDB Buffer Pool: Set to 70-80% of RAM
  2. Binary Logging: Use ROW format
  3. Query Cache: Disable (deprecated in 8.0)
  4. Connection Pooling: Use ProxySQL

See Also