In this post, we'll explore how to implement a simple but powerful caching system in N8N using a Code Tool node. This solution is particularly useful when working with rate-limited APIs or when you need to optimize workflow performance.
The Problem
When building workflows in N8N, we often face scenarios where we:
- Make repeated API calls to get the same data
- Hit rate limits with external APIs
- Need to optimize workflow performance
- Want to save temporary data between workflow runs
The Solution
We'll build a versatile caching system that can:
- Store and retrieve any data with a key
- Handle JSON objects
- Automatically expire cache after 24 hours
- Provide cache management functions
2. Rate Limit Management
// Store rate limit status
{
"query": {"rate_limit_github": "remaining_calls: 4999"}
}
// Check before making calls
{
"query": "rate_limit_github"
}
3. Temporary Data Storage
// Store processing status
{
"query": {"job_status_123": "processing"}
}
// Update when done
{
"query": {"job_status_123": "completed"}
}
4. User Preferences
// Store user settings
{
"query": {"user_123_preferences": {"theme": "dark", "language": "en"}}
}
Cache Management
View Cache Contents
{
"query": "show"
}
Output:
api_response_12345: API_RESPONSE_DATA
rate_limit_github: remaining_calls: 4999
job_status_123: completed
Delete Cache Entry
{
"query": "delete api_response_12345"
}
Integration Examples
Example 1: API Rate Limit Protection
- Check cache for rate limit status
- If approaching limit, wait
- Make API call
- Update rate limit cache
- Cache API response
Example 2: Smart Data Refresh
- Check cache for data
- If expired, fetch new data
- Update cache
- Continue workflow
Best Practices
Key Naming: Use consistent key naming patterns:
api_response_[endpoint]user_[id]_[data_type]job_[id]_[status]
Cache Duration: Modify the TTL based on your needs:
CACHE_TTL = 24 * 60 * 60 # 24 hours in seconds
Error Handling: Always have fallback logic for cache misses:
if (cached_response) {
// Use cached data
} else {
// Fetch fresh data
}
Conclusion
This simple caching system can significantly improve your N8N workflows by:
- Reducing API calls
- Improving response times
- Managing rate limits
- Providing temporary storage
Remember to adjust the cache duration and storage method based on your specific needs. For production environments, you might want to consider using Redis or a database for more robust caching.
Next Steps
- Add cache compression for large datasets
- Implement cache versioning
- Add batch operations
- Consider distributed caching for multi-node setups
Feel free to modify and expand upon this implementation for your specific use cases!

SOCIAL SHARE CARD GENERATOR