Performance Optimization
Strategies for optimizing performance on the LegionEdge Platform
This guide covers techniques for reducing latency, minimizing API calls, and optimizing resource usage on the LegionEdge Platform.
Caching Strategies
SDK-Level Caching
The SDK includes a built-in in-memory cache for read operations:
import { LegionEdge } from "@legionedge/sdk";
const client = new LegionEdge({
apiKey: process.env.LEGIONEDGE_API_KEY!,
cache: {
enabled: true,
ttl: 60_000, // default TTL: 60 seconds
maxEntries: 5_000,
strategy: "lru", // least recently used eviction
},
});
// First call hits the API
const project = await client.projects.get("proj_abc123");
// Second call returns from cache (within TTL)
const projectAgain = await client.projects.get("proj_abc123");Per-Request Cache Control
Override caching behavior on individual requests:
// Force a fresh fetch, bypassing cache
const project = await client.projects.get("proj_abc123", {
cache: "no-cache",
});
// Use cached data even if stale (useful for fallback)
const project = await client.projects.get("proj_abc123", {
cache: "force-cache",
});External Cache Integration
For distributed applications, integrate with Redis or Memcached:
import { LegionEdge, RedisCacheAdapter } from "@legionedge/sdk";
import Redis from "ioredis";
const redis = new Redis(process.env.REDIS_URL!);
const client = new LegionEdge({
apiKey: process.env.LEGIONEDGE_API_KEY!,
cache: {
adapter: new RedisCacheAdapter(redis, {
keyPrefix: "le:",
ttl: 300_000, // 5 minutes
}),
},
});Query Optimization
Use Filters and Pagination
Always filter server-side rather than fetching all data and filtering in memory:
// Fetch only active projects, sorted by most recently updated
const projects = await client.projects.list({
organizationId: orgId,
status: "active",
sort: "updated_at",
order: "desc",
perPage: 25,
});Select Specific Fields
Reduce response payload size by requesting only necessary fields:
const projects = await client.projects.list({
organizationId: orgId,
fields: ["id", "name", "status", "updatedAt"],
});Use Batch Endpoints
Combine multiple requests into a single API call:
// Fetch multiple projects in one request
const projects = await client.projects.batchGet([
"proj_abc",
"proj_def",
"proj_ghi",
]);
// Batch update
await client.resources.batchUpdate([
{ id: "res_1", config: { instances: 3 } },
{ id: "res_2", config: { instances: 2 } },
]);Connection Pooling
HTTP Keep-Alive
The SDK enables HTTP keep-alive by default, reusing connections across requests:
const client = new LegionEdge({
apiKey: process.env.LEGIONEDGE_API_KEY!,
connection: {
keepAlive: true,
maxSockets: 50,
timeout: 30_000,
},
});CDN and Edge Caching
For applications serving public content sourced from the platform, leverage CDN caching:
// API responses include cache-control headers
// GET /v2/projects/proj_abc123
// Cache-Control: public, max-age=60, stale-while-revalidate=30
// In your application, forward these headers to your CDN
app.get("/api/project/:id", async (req, res) => {
const project = await client.projects.get(req.params.id);
res.setHeader("Cache-Control", "public, max-age=60, stale-while-revalidate=30");
res.json(project);
});Monitoring Performance
Use the SDK's built-in metrics to track API performance:
const client = new LegionEdge({
apiKey: process.env.LEGIONEDGE_API_KEY!,
hooks: {
afterResponse: (response) => {
const duration = response.headers.get("x-response-time");
metrics.histogram("legionedge.api.duration", parseFloat(duration!), {
endpoint: response.url,
status: response.status.toString(),
});
return response;
},
},
});Next Steps
- Review best practices for additional optimization tips.
- Set up webhooks to replace polling with event-driven updates.