Simple In-Memory Data Caching System using Data Tables - n8n Workflow

Create a robust, time-to-live (TTL) aware data cache using n8n Data Tables. This reusable n8n workflow template handles reads, writes, and automated cleanup.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Automation Architects: Building reusable infrastructure components within n8n. Developers: Needing to minimize API calls or manage state across multiple n8n workflow executions.* Performance Enthusiasts: Looking to implement rapid, self-hosted data lookup mechanisms within their n8n templates.

Overview

This advanced n8n workflow template demonstrates how to build a vital piece of infrastructure: a reliable data cache, without relying on external services like Redis. This specific n8n workflow utilizes the powerful n8n Data Table n8n node in an 'upsert' (write) and 'get' (read) manner to manage key-value pairs with an expiration (TTL).

Implementing this cache is essential for minimizing API calls, improving the performance of resource-intensive n8n templates, and reducing external service costs. This complete n8n solution includes two key operational components: a sub-workflow n8n trigger for on-demand cache operations (read/write) and an independent hourly n8n trigger for automated cache cleanup and maintenance.

Important Setup Note: This n8n workflow requires an n8n Data Table named cache with columns key (string), ttl (datetime), and value (string).

How it Works

The core logic of this n8n workflow is split into a sub-workflow execution path and a cleanup path.

Read/Write Execution Path


  1. Trigger & Routing: The n8n workflow begins with the 'When Executed by Another Workflow' n8n trigger, expecting inputs like cacheKey, writeValue, and trueToWrite. The 'Check Action Type' n8n node determines if the request is a 'write' (if trueToWrite is true) or a 'read' operation.

  2. Write Operation: If writing, the n8n workflow proceeds to the 'Upsert row(s)' Data Table n8n node. It updates the existing entry or creates a new one, stringifying the writeValue and calculating the ttl by adding the input writeTTLms to the current time ($now). The written value is then returned.

  3. Read Operation: If reading, the n8n workflow uses the 'Get row(s)' Data Table n8n node to retrieve the entry based on the cacheKey.

  4. Validation: Two sequential 'If' n8n nodes ('If not in Cache' and 'If Expired Cache') validate the entry. If the entry is not found or if the stored ttl is before the current $now, the flow proceeds to 'No cache found...' (Stop and Error), signaling the calling n8n workflow that the cache needs to be refreshed.

  5. Return: If the entry is valid and not expired, the 'Return Value from Cache' n8n node parses the JSON string back into its original data structure and returns it.

Hourly Cleanup Path


  1. Scheduled Maintenance: The '1 Hour Clean for Cache Table' n8n trigger runs automatically every hour.

  2. Data Deletion: It connects directly to the 'Drop all rows with expired cache entires' Data Table n8n node, which executes a batch deleteRows operation where the ttl field is less than the current date and time ($now). This keeps the underlying Data Table small and performant.

Installation Guide


  1. Prerequisites: Ensure you have access to the n8n Data Tables feature (potentially a beta feature depending on your n8n version).

  2. Import: Import the provided n8n workflow JSON into your n8n instance.

  3. Data Table Setup: Create an n8n Data Table named cache (all lowercase) and ensure it has the following exact columns:

key: string
ttl: datetime
* value: string

  1. Node Update: Open both 'Upsert row(s)' and 'Get row(s)' n8n nodes. Verify that the 'Data Table ID' field correctly links to your newly created cache table.

  2. Activation: The workflow can be used immediately as a sub-workflow. To enable the automatic cleanup, activate the entire n8n workflow using the toggle in the top bar.

Node Details

When Executed by Another Workflow (n8n trigger): The entry point for cache operations. It defines the necessary inputs (cacheKey, writeValue, trueToWrite, writeTTLms) required by the calling n8n workflow.
Check Action Type (If n8n node): Determines if the sub-workflow call is a read or a write based on the input boolean trueToWrite.
Upsert row(s) (Data Table n8n node): Handles cache writing. It uses expressions to set the key for matching and calculates the ttl by adding the writeTTLms input to $now.
Get row(s) (Data Table n8n node): Handles cache reading by filtering the Data Table based on the incoming cacheKey.
If not in Cache / If Expired Cache (If n8n node): Core flow control nodes that enforce cache validation. They check for the existence of the row and compare the stored ttl value against $now to determine validity.
No cache found, use error detection to detect this. (Stop and Error n8n node): Used to terminate the n8n workflow with a specific error message if the requested key is missing or expired, allowing the parent n8n workflow to handle the cache miss.
1 Hour Clean for Cache Table (Schedule n8n trigger): An independent n8n trigger that runs maintenance tasks hourly.
Drop all rows with expired cache entires (Data Table n8n node): Performs the cleanup operation, deleting rows where the ttl is less than the current time.

Related n8n Workflows

Free

Nodes: 8 Nodes
Updated: December 26 2025
View all
Created by
JustCallMeBlue
JustCallMeBlue

Featured*