# Documenso Performance Tuning
## Overview
Optimize Documenso integrations for speed, efficiency, and scalability.
## Prerequisites
- Working Documenso integration
- Performance monitoring in place
- Redis or caching layer (recommended)
## Caching Strategies
### Step 1: Document Metadata Cache
```typescript
import Redis from "ioredis";
const redis = new Redis(process.env.REDIS_URL);
const CACHE_TTL = 300; // 5 minutes
interface CachedDocument {
id: string;
title: string;
status: string;
recipientCount: number;
cachedAt: string;
}
async function getDocumentCached(
documentId: string
): Promise {
// Try cache first
const cacheKey = `doc:${documentId}`;
const cached = await redis.get(cacheKey);
if (cached) {
return JSON.parse(cached);
}
// Fetch from API
const client = getDocumensoClient();
const doc = await client.documents.getV0({ documentId });
// Cache the result
const cacheData: CachedDocument = {
id: doc.id!,
title: doc.title!,
status: doc.status!,
recipientCount: doc.recipients?.length ?? 0,
cachedAt: new Date().toISOString(),
};
await redis.setex(cacheKey, CACHE_TTL, JSON.stringify(cacheData));
return cacheData;
}
// Invalidate cache on webhook events
async function invalidateDocumentCache(documentId: string): Promise {
await redis.del(`doc:${documentId}`);
}
```
### Step 2: Template Cache
```typescript
// Templates change rarely - longer TTL
const TEMPLATE_CACHE_TTL = 3600; // 1 hour
async function getTemplateCached(templateId: string) {
const cacheKey = `tmpl:${templateId}`;
const cached = await redis.get(cacheKey);
if (cached) {
return JSON.parse(cached);
}
const client = getDocumensoClient();
const template = await client.templates.getV0({ templateId });
await redis.setex(cacheKey, TEMPLATE_CACHE_TTL, JSON.stringify(template));
return template;
}
// Bulk cache templates on startup
async function warmTemplateCache(templateIds: string[]): Promise {
const client = getDocumensoClient();
console.log(`Warming cache for ${templateIds.length} templates...`);
for (const templateId of templateIds) {
try {
await getTemplateCached(templateId);
} catch (error) {
console.warn(`Failed to cache template ${templateId}`);
}
}
}
```
## Batch Operations
### Step 3: Batch Document Creation
```typescript
import PQueue from "p-queue";
interface DocumentInput {
title: string;
templateId: string;
recipients: Array<{ email: string; name: string }>;
}
async function batchCreateDocuments(
inputs: DocumentInput[]
): Promise