How to Implement Caching for Chatbot in n8n to Reduce Vector Database Queries?

Title: How to Implement Caching for Chatbot in n8n to Reduce Vector Database Queries?

Hi n8n Community,

I’m currently working on a chatbot workflow in n8n that uses a vector database (PostgreSQL with pgvector) to retrieve answers based on user queries. Here’s a quick overview of my setup:

  1. Workflow Trigger:
    The workflow starts with a When Chat Message Received node.
  2. Query Handling:
    The user query is sent to a vector database, which performs a semantic search to retrieve the best-matching response.
  3. Challenge:
    Every time a user sends a query, the workflow queries the vector database, even if the same query has already been processed. This leads to unnecessary database queries and increased token usage, especially when the same or similar queries are asked repeatedly.

What I Want to Achieve:

I’d like to implement caching in my chatbot workflow, so that:

  1. If a user sends a query that has already been processed, the workflow can return the cached response without querying the vector database again.
  2. If the query is new, the workflow queries the database, stores the response in the cache, and returns it to the user.

Below is my current workflow:

Information on your n8n setup

  • n8n version:: Community Version, 1.71.3
  • Database (Pg Vector with Postgresql):
  • n8n EXECUTIONS_PROCESS setting (default):
  • Running n8n via (Docker):
  • Operating system:: Windows 64-bit operating system

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:
  • **n8n version: Community Version, 1.71.3 **
  • Database (default: PostgreSql):
  • n8n EXECUTIONS_PROCESS setting (default: main):
  • Running n8n via : Docker
  • Operating system: Windows 64-bit operating system

I would use Redis as a cache, you’ll need to implement logic as to when it should and shouldn’t read the cache, so you’ll need sub-workflow rather than connecting the vector store as a tool.

1 Like