Vector Databases

Vespa for Real-Time Fraud Detection: Combining Vector Similarity with Structured Transaction Data

In the high-stakes world of financial transactions, speed and precision are not just desirable; they are mandatory. Traditional rule-based fraud detection systems often struggle with the nuance and evolving patterns of modern fraud. This is where the power of hybrid search comes into play. By leveraging Vespa, an open-source, real-time serving engine, we can combine the structured query capabilities of traditional databases with the fuzzy, semantic power of vector similarity search to identify anomalies in milliseconds.

Why Hybrid Search? The Limitations of Pure Rules

Most existing fraud detection pipelines rely on hardcoded rules: "If amount > $10,000 AND country != home_country, flag." While effective for obvious red flags, these rules fail to catch sophisticated fraud where individual attributes look normal, but the combination or context is anomalous.

Vector similarity search addresses this by embedding transaction features into a high-dimensional space. Similar transactions cluster together. However, vectors alone lack precision for hard constraints (like exact timestamp ranges or specific merchant IDs). Vespa shines here because it natively supports tensor indexing alongside standard attributes, allowing you to perform vector searches constrained by structured filters.

Architecting the Solution in Vespa

To implement this, we define a schema in Vespa that includes both structured fields (amount, time, user_id) and a tensor field for the embedded representation of the transaction context.


schema fraud_transactions {
    document fraud_transactions {
        field user_id type string {
            indexing: index summary
        }
        field amount type double {
            indexing: attribute
        }
        field timestamp type long {
            indexing: attribute
        }
        field transaction_embedding type tensor<float(x)> {
            indexing: index
            index:
                distance: dot
                max-search-results: 10000000
        }
    }
}

Notice the distance: dot configuration. For fraud detection, cosine similarity (often implemented via dot product on normalized vectors) is preferred to measure directional similarity in behavior.

Real-Time Querying with Hybrid Logic

The magic happens at query time. When a new transaction arrives, we embed it into a vector and query Vespa. We ask the engine: "Find transactions similar to this vector, BUT only among those that occurred in the last 24 hours for this specific user segment."

Here is a practical example of a Vespa query language (VQL) statement:


query {
    id: "my-fraud-query"
    hits: 10
    timeout: 50ms
}
{
    user_id: "user_12345"
    timestamp: "range(1700000000, 1700086400)"
    embedding: "0.1, 0.5, 0.3, ..." // The vector of the incoming transaction
}

Vespa executes this in parallel. The structured filters prune the search space dramatically, ensuring the vector search operates on a highly relevant subset of data. This reduces the computational overhead compared to searching the entire corpus and significantly improves latency.

Practical Example: Detecting Account Takeover

Consider an Account Takeover (ATO) scenario. A hacker might use a stolen password but transact from a different device or location. The amount might be normal, and the user_id is correct (structured fields pass). However, the embedding, which might encode device fingerprint, IP geolocation, and typing cadence, will be significantly different from the user's historical normal vectors.

By calculating the similarity score against the user's historical transaction vectors (stored in Vespa), we can assign a fraud risk score. If the maximum similarity score is below a threshold (e.g., 0.85), the transaction is flagged for manual review or automatic block.

Performance Considerations

To maintain sub-100ms latency for real-time fraud checks, consider the following Vespa best practices:

  • Dimensionality Reduction: Use PCA or UMAP to reduce vector dimensions from 768 to 128-256 before indexing. This reduces memory footprint and speeds up distance calculations.
  • Indexing Strategy: Use hnsw (Hierarchical Navigable Small World) for approximate nearest neighbor search if the dataset exceeds millions of vectors. Vespa supports this via the indexing: attribute and specific index configurations.
  • Caching: Enable query caching for frequent user segments to avoid redundant vector calculations.

Conclusion

Integrating vector similarity with structured data in Vespa provides a robust framework for next-generation fraud detection. It moves beyond rigid rules to a context-aware, probabilistic model that adapts to evolving fraud patterns. For developers and data engineers looking to build real-time AI applications, Vespa offers the rare combination of SQL-like query power, millisecond latency, and deep tensor indexing capabilities. By mastering this hybrid approach, you can build systems that are not just fast, but truly intelligent.

Share: