Performance & Best Practices
This guide covers strategies for optimizing your use of the SensorUp GraphQL API to achieve the best performance and reliability.Query Optimization
Request Only What You Need
GraphQL’s strength is precise field selection. Use it: Bad:Use Pagination Appropriately
Don’t request more data than you can display: Bad:- UI lists: 20-50 items
- Background processing: 100-500 items
- Exports: Use export endpoints, not pagination
Batch Related Queries
GraphQL allows querying multiple resources in one request: Bad:Use Fragments for Reusability
Define reusable fragments for common field sets:Federation Performance
Understand Query Plans
Cross-subgraph queries require multiple fetches. Minimize hops: Less Efficient:Leverage Parallel Execution
Federation executes independent root fields in parallel:Filtering & Searching
Use Server-Side Filtering
Filter on the server, not the client: Bad:Specific Lookups vs. List Filtering
Use ID lookups when you know the ID: Less Efficient:Caching Strategies
Cache Static Data
Some data changes infrequently:| Data Type | Change Frequency | Cache Duration |
|---|---|---|
| Asset Profiles | Rarely | 1 hour - 1 day |
| Catalog Entries | Rarely | 1 day - 1 week |
| Form Templates | Occasionally | 1 hour |
| Map Configurations | Rarely | 1 day |
| User Session | Often | No cache (always fetch) |
| Assets | Often | No cache or short (5 min) |
HTTP Caching
Configure appropriate cache headers:Application-Level Caching
Use a caching client like Apollo Client:Rate Limiting & Throttling
Implement Exponential Backoff
When encountering rate limits or errors:Batch Updates
Instead of many small mutations, batch when possible: Less Efficient:Query Complexity
Avoid Deep Nesting
Deep queries can be expensive: High Complexity:Limit List Sizes
Always use pagination limits:Monitoring & Debugging
Use Correlation IDs
Include correlation IDs for tracking:Log Query Performance
Track slow queries:Monitor Error Rates
Track and alert on error patterns:Network Optimization
Use Compression
Enable gzip/brotli compression:Minimize Payload Size
- Use fragments to avoid duplication
- Omit
__typenameif not needed - Use aliases to simplify response structure
Connection Pooling
Reuse HTTP connections:Best Practices Checklist
Query Design
- Request only needed fields
- Use pagination (first: 20-100)
- Apply server-side filters
- Use specific lookups vs. list queries
- Batch related queries in one request
- Use fragments for reusability
Performance
- Cache static data (profiles, catalogs)
- Implement exponential backoff
- Monitor query performance
- Use correlation IDs
- Minimize cross-subgraph hops
- Limit query depth (≤ 4 levels)
Error Handling
- Check GraphQL-level errors
- Check mutation-level errors
- Log errors with context
- Implement retry logic
- Handle partial failures gracefully
Security
- Store credentials securely
- Rotate API keys regularly
- Use HTTPS always
- Validate input data
- Sanitize user-provided content
Performance Metrics
Target Metrics
| Metric | Target | Good | Needs Improvement |
|---|---|---|---|
| Query latency (p50) | < 200ms | < 500ms | > 500ms |
| Query latency (p99) | < 1s | < 2s | > 2s |
| Error rate | < 0.1% | < 1% | > 1% |
| Cache hit rate | > 80% | > 60% | < 60% |
Measuring Performance
Common Anti-Patterns
1. Over-fetching
2. N+1 in Application Code
3. Polling Too Frequently
4. Ignoring Errors
Resources
- Federation Concepts - Understanding query planning
- Common Patterns - API patterns and conventions
- Apollo Client Performance - Client-side optimization
- GraphQL Best Practices - General GraphQL guidelines
Support
For performance issues or optimization assistance:- Review query plans in Apollo Studio
- Contact your SensorUp account team
- Provide correlation IDs for specific slow queries