⌨ Keyboard shortcuts available
G — waiting for next key…
FastAPI pgvector 14 min read

The First-Mover Advantage: Building an Event-Driven RAG Architecture with RudderStack Webhooks

Move from stale cron jobs to real-time context. Learn how to build an event-driven RAG architecture using RudderStack webhooks, FastAPI, and pgvector for reactive user embeddings.

P

Pradeep Bhandari

 · 49 views

Stop waiting for users to ask a question before gathering context. If your setup relies on daily cron jobs to sync your vector store, your RAG system is operating on stale data. You need an event-driven RAG architecture where every click, purchase, or support ticket immediately updates the user's semantic profile. Every event in your RudderStack stream is a signal that should be reflected in your vector store in real-time, not hours later.

Instead of a passive knowledge base, build a reactive one. Pipe behavioral events through RudderStack webhooks into a listener that transforms actions into fresh embeddings. A user browsing high-end cameras should trigger an update to their pgvector representation within milliseconds. If your HNSW index isn't updating in rhythm with your traffic, your LLM is working in a vacuum.

This guide covers webhook concurrency, high-velocity ingestion, and filtering. When traffic surges, your database cannot afford to choke. We will map a transformer layer to filter noise, configure pgvector for rapid updates, and handle race conditions inherent in high-velocity event streams.

Why wait for the question? The case for pre-emptive context

Most RAG implementations are "lazy," fetching data only when an LLM call is triggered. By moving context-building "left" in the data pipeline, you treat embeddings as a live mirror of user behavior. Every track or identify call from RudderStack—page views, feature clicks, or support tickets—should be piped into your embedding model the moment it hits your gateway.

Consider this payload. It is the raw material for your next retrieval step:

{  "event": "Documentation Viewed",  "userId": "user_8821",  "properties": {    "title": "Configuring pgvector HNSW Indexes",    "category": "Postgres",    "tags": ["indexing", "vector-search", "performance"]  },  "timestamp": "2026-02-15T10:00:00Z"}

By running this through a Python-based FastAPI listener that generates an embedding and pushes it to pgvector, you ensure that when the LLM is eventually queried, the most relevant user context is already indexed and ready.

Setting the stage: Infrastructure and prerequisites

A real-time reactive system requires a high-performance foundation.

PostgreSQL and pgvector setup

Use a Postgres 15+ instance. Enable the vector extension to support high-performance vector operations:

CREATE EXTENSION IF NOT EXISTS vector;

Define a table for user embeddings. Ensure the vector dimensions match your model (e.g., 1536 for OpenAI text-embedding-3-small):

CREATE TABLE user_behavior_vectors (    id uuid PRIMARY KEY DEFAULT gen_random_uuid(),    user_id text NOT NULL,    context_vector vector(1536),    metadata jsonb,    last_updated timestamptz DEFAULT now());

Python environment and RudderStack source

Use FastAPI for the ingestion layer to handle asynchronous webhook bursts. You will need pgvector, psycopg[binary,pool], and openai. In your RudderStack dashboard, create a Source (JavaScript or Python SDK) to capture track() calls. If developing locally, use a tool like ngrok to provide RudderStack with a public URL for your FastAPI listener.

Step 1: Configuring the RudderStack Webhook destination

Point the RudderStack firehose at your FastAPI endpoint. In the RudderStack dashboard, create a new Webhook destination. Set the URL to your public endpoint, use the POST method, and enable the destination.

Payload filtering with transformations

Sending raw event data to an embedding model increases latency and pollutes your vector space. Use a User Transformation in RudderStack to strip PII and irrelevant metadata. Focus on high-signal interactions:

export function transformEvent(event) {  if (event.type !== 'track') return null;  const allowedProperties = ['product_name', 'category', 'search_term', 'action_type'];  const filteredProps = {};  if (event.properties) {    allowedProperties.forEach(prop => {      if (event.properties[prop]) {        filteredProps[prop] = event.properties[prop];      }    });  }  return {    messageId: event.messageId,    userId: event.userId || event.anonymousId,    event: event.event,    properties: filteredProps,    timestamp: event.originalTimestamp  };}

Use the Live Events tab to verify the cleaned-up JSON. The goal is a minimal payload that captures user intent without the noise of browser state.

Step 2: Building the event-driven RAG architecture listener

The listener acts as the gateway converting behavioral signals into vector context. Bottlenecks here will choke the pipeline.

FastAPI and Pydantic validation

FastAPI handles asynchronous operations natively and uses Pydantic for strict data validation. Define a schema that mirrors your filtered RudderStack payload:

from fastapi import FastAPI, BackgroundTasks, Response, statusfrom pydantic import BaseModel, Fieldfrom typing import Dict, Anyapp = FastAPI()class RudderEvent(BaseModel):    user_id: str = Field(..., alias="userId")    action_type: str = Field(..., alias="event")    properties: Dict[str, Any]async def process_vector_update(event: RudderEvent):    # Logic for embedding and pgvector updates    pass@app.post("/webhooks/rudderstack", status_code=status.HTTP_200_OK)async def ingest_behavioral_data(    event: RudderEvent,     background_tasks: BackgroundTasks):    # Offload processing to background workers immediately    background_tasks.add_task(process_vector_update, event)    return {"message": "Event received and queued"}

Using background_tasks.add_task allows you to return a 200 OK to RudderStack immediately, preventing timeouts and retry storms during intensive vector calculations.

Step 3: Real-time vector updates with pgvector

To maintain a living snapshot of user intent, you must turn JSON into semantic representations immediately.

Generating embeddings on the fly

Map actions to descriptive strings. For a page_viewed event, construct a sentence describing the action. Pass this to your embedding model. For low latency, use local models like sentence-transformers; for higher semantic nuance, use OpenAI’s text-embedding-3-small.

import openai def get_embedding(text):    response = openai.Embedding.create(        input=text,        model="text-embedding-3-small"    )    return response['data'][0]['embedding']user_action_desc = f"User {payload['userId']} interacted with {payload['properties']['feature_name']}"vector = get_embedding(user_action_desc)

The upsert logic

Avoid creating a massive, redundant haystack of vectors. Treat pgvector like a high-speed cache of user state. Use the ON CONFLICT clause to ensure each new event refreshes the existing user vector, keeping the index lean.

insert_query = """INSERT INTO user_embeddings (user_id, embedding, last_updated)VALUES (%s, %s, NOW())ON CONFLICT (user_id) DO UPDATE SET     embedding = EXCLUDED.embedding,    last_updated = EXCLUDED.last_updated;"""cursor.execute(insert_query, (payload['userId'], vector))connection.commit()

Step 4: Handling concurrency and race conditions

Parallel processing of event streams can lead to "dirty writes" where older data overwrites newer data due to network jitter.

Ensuring chronological integrity

Use RudderStack's received_at timestamp as the source of truth. Before committing an update, verify that the incoming event is newer than the existing record. Additionally, use a Redis-based distributed lock (mutex) around the user_id to prevent simultaneous recalculations for the same user.

import redisfrom datetime import datetimer = redis.Redis(host='localhost', port=6379, db=0)def update_user_vector(user_id, event_timestamp, new_vector):    lock_key = f"lock:user_profile:{user_id}"        with r.lock(lock_key, timeout=5):        last_update = db.execute(            "SELECT last_event_at FROM user_profiles WHERE user_id = %s",             (user_id,)        ).fetchone()        if last_update and event_timestamp <= last_update['last_event_at']:            return {"status": "ignored", "reason": "stale_event"}        db.execute(            "UPDATE user_profiles SET embedding = %s, last_event_at = %s WHERE user_id = %s",            (new_vector, event_timestamp, user_id)        )        return {"status": "success"}

Step 5: pgvector optimization for high-velocity ingestion

Streaming data requires a different indexing strategy than batch loads. Standard indexing can crumble under constant upserts.

Index maintenance: Using HNSW

For real-time streams, use HNSW (Hierarchical Navigable Small Worlds). Unlike IVFFlat, HNSW supports incremental updates, meaning new vectors are searchable immediately upon transaction commit.

CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)WITH (m = 16, ef_construction = 64);

Adjust the m parameter (connections per layer) based on RAM and recall needs. To handle the "Postgres tax" (dead tuples from frequent updates), tune autovacuum settings for your vector tables to keep the index from fragmenting:

ALTER TABLE user_embeddings SET (  autovacuum_vacuum_scale_factor = 0.01,  autovacuum_vacuum_cost_limit = 1000);

Troubleshooting the asynchronous RAG pipeline

When the pipeline hits bottlenecks, check the following:

  • Idempotency: Use the RudderStack messageId as a unique key to prevent duplicate embeddings during retries.
INSERT INTO user_embeddings (event_id, user_id, embedding)VALUES (:eid, :uid, :vector)ON CONFLICT (event_id) DO NOTHING;
  • Dimension Mismatches: If swapping embedding models, you must migrate to a new column; vector dimensions cannot be mixed or cast.
  • Webhook Timeouts: If using scale-to-zero infrastructure, offload the raw payload to a message queue (like RabbitMQ or Redis) and return 202 Accepted immediately to avoid RudderStack 504 errors.

So, is your RAG living in the past?

Waiting for batch refreshes means your AI is effectively hallucinating based on history rather than current intent. Real-time event streams bridge this gap. By piping RudderStack events directly into pgvector, you create a living reflection of the user journey.

A decoupled, event-driven setup also provides resilience. Treating updates as independent, queued events insulates your application from embedding provider latency or database indexing spikes. Getting the plumbing right—HNSW tuning and concurrency management—is the difference between a static demo and a production-ready relevance engine. Don't feed your LLM yesterday's news.

Sources & Further Reading

Frequently Asked Questions

Why is HNSW better than IVFFlat for real-time RAG updates?

Unlike IVFFlat, which typically requires rebuilding the index to maintain search quality after significant data changes, HNSW supports incremental updates. This means new vectors are searchable immediately upon transaction commit, making it ideal for event-driven RAG architectures where the database is under constant load. HNSW maintains high recall without the Postgres tax of frequent dead tuples, provided you tune autovacuum settings correctly to prevent fragmentation during high-velocity event ingestion.

How do you handle race conditions in a real-time embedding pipeline?

To prevent dirty writes where network jitter causes older events to overwrite newer data, you should use the event's original timestamp as the source of truth. Before committing a pgvector update, check if the incoming event is newer than the stored record. Additionally, implementing a distributed lock using a tool like Redis around the user ID ensures that simultaneous updates for the same profile are processed sequentially, maintaining chronological integrity across the event stream.

Share this article

Related Articles

Discussion

No comments yet — be the first to share your thoughts.

Leave a comment

Comments are moderated before appearing.

Max 2,000 characters · not published

We respect your privacy