TL;DR
- The whole platform runs on managed Aiven services plus four small programs: a fetcher, a chunk-and-embed worker, an API and an eval runner.
- Kafka Connect does all the indexing. The JDBC sink writes vectors into pgvector through a generated column, and the OpenSearch sink needs its index created first.
- Hybrid search improves relevance, and only a per-source quota settles which of two correct documents wins.
- A semantic answer cache is unsafe: the question with the opposite answer measured closer (0.122) than the true paraphrase (0.145).
- The small encoder is owned and runs on CPU. The large decoder is rented through the Aiven AI Gateway.
Part 1 of a three-part series on building a documentation assistant with Aiven. Part 0 gave you the vocabulary. This part is the design: which service does what, why, and what the data looks like at every hop. Part 2 builds it and measures it.
In Part 0 we followed one question, "How do I add partitions to a Kafka topic?", through an encoder, two searches, a prompt and a decoder. That was the mechanics. Now we have to decide where each of those steps runs, and what happens to the data between them.
I'll give you the conclusion first. The whole platform fits on managed Aiven services plus four small programs of ours. Before writing this article I tested the risky joints on real services, so wherever you read "this works", there is a test behind it. Wherever something is still a design choice waiting for a measurement, I say so.
What we are building
A support assistant that answers questions about running Apache Kafka®, PostgreSQL® and OpenSearch® on Aiven. It reads two kinds of documentation: Aiven's own docs, and the upstream docs of each open source project.
Mixing the two is what makes the problem interesting. Ask "how do I change max_connections in PostgreSQL?" and the upstream manual tells you to edit postgresql.conf and restart. That is correct. On a managed service you have no access to that file, and the right answer is a setting in the service configuration. That is correct too. Both documents are right, and an assistant that picks the wrong one sends a user looking for a file that does not exist for them.
Here are three of these pairs, the kind the assistant meets all day:
| Question | Upstream docs say | Aiven docs say |
|---|---|---|
| Change a PostgreSQL setting | Edit postgresql.conf or run ALTER SYSTEM | Set it in the service's advanced configuration |
| Add partitions to a Kafka topic | Run kafka-topics.sh --alter against a broker | Use the console, the API or Terraform |
| Get superuser rights in PostgreSQL | Connect as a superuser role | avnadmin is an admin role without superuser |
So the design has one requirement that an ordinary RAG tutorial does not have. Every passage must carry where it comes from, and the system must use that when two passages disagree. I call the field source_authority, with two values: platform and upstream. You will see it in every schema below.
The architecture in one diagram
Read it as three paths.
Indexing, in batch. A fetcher clones the documentation repositories and publishes one event per page. A worker cuts each page into chunks, embeds them, and publishes the result. Kafka Connect then writes every chunk into PostgreSQL and into OpenSearch. Nothing here is urgent. If it runs an hour late, nobody notices.
Answering, online. A question arrives at the API. The API embeds it, searches both stores, merges the results, builds a prompt and calls a decoder through the Aiven AI Gateway. A user is waiting, so every millisecond counts here.
Observing, always. Each answer produces one trace event: the question, the passages used, the timings, the token counts. Those events land in ClickHouse®, and Grafana® reads them. A small job scores a sample of answers and publishes the scores to the same place.
The two first paths never call each other. They only share the stores. That separation is the reason there is a Kafka cluster in the middle, and it is worth a few lines.
Service by service, and why not something simpler
Apache Kafka, with Schema Registry. You could index without it: a script that reads the docs and writes straight into the database. It works on day one. Then you change the chunk size, or the embedding model, and you have to re-run everything from the Git clone. With the pages kept in a topic, re-indexing is a consumer reading from offset zero. The topic also gives every stage a contract. The fetcher does not know who reads its events, and the worker does not care who wrote them.
The topics are classic topics. Aiven for Apache Kafka also offers diskless topics, built for high-throughput streams. A documentation corpus is a few thousand events, so they would bring nothing here.
Our code on Aiven Runtime. Four programs, each one small:
- the fetcher, a job that clones the doc repositories, converts each page to plain text (PostgreSQL ships its manual in SGML, which needs its own conversion), computes a hash of the content and records the licence;
- the chunk-and-embed worker, which holds the encoder;
- the API, which answers questions;
- the eval runner, which scores answers after the fact.
Why not a Kafka Connect source connector for the Git repositories? There is no managed one, and adding a custom connector means packaging and deploying it yourself. A sixty-line job is easier to read, to run and to explain.
Kafka Connect for indexing. This was my main doubt, so it was the first test. Can the managed JDBC sink write a vector into pgvector, with no custom code? Yes, with one trick that I show in the contracts section. Can the managed OpenSearch sink write into a knn_vector field? Yes, on one condition about ordering. The result is that we write no indexing code at all. An earlier version of this design had an "indexer" worker. The tests removed it.
PostgreSQL with pgvector, and OpenSearch. Two stores for the same chunks, because they are good at different things. pgvector finds passages that mean the same thing as the question. OpenSearch finds passages that contain the same strings, such as kafka-topics.sh or an error code, which embeddings are poor at. Why not one engine for both? OpenSearch can do the two searches on its own, and it does them well. I tested that as well. I keep both because most teams already have their application data in PostgreSQL, and because Part 2 compares the two setups with numbers so you can choose.
Valkey™. An exact-match cache for answers, and rate limiting. Nothing more, for a reason that has its own section below.
Aiven AI Gateway. The decoder is the one part we do not host. The gateway exposes an OpenAI-compatible chat completions endpoint, so the API talks to it with a standard client. At the time of writing the gateway is in early access. If you do not have it yet, any OpenAI-compatible endpoint works as a stand-in, including a local Ollama while you develop.
ClickHouse and Grafana. Trace events are append-only, wide, and queried by time range and aggregation. That is a columnar workload. Aiven for ClickHouse reads the topic through its Kafka integration and a materialised view, so again there is no consumer to write.
Two engines, four searches
When a question arrives, the API sends its words to OpenSearch and its vector to PostgreSQL. Each engine is asked twice: once filtered on source_authority = platform, once on upstream. That makes four small result lists.
Then the API merges them with reciprocal rank fusion. The idea is simple. A passage earns 1 / (60 + rank) for each list where it appears, and the scores are added. A passage that is second in the keyword list and third in the vector list gets 1/62 + 1/63. Only ranks are used, which avoids a real problem: a BM25 score of 14.2 and a cosine similarity of 0.88 have no common scale, and any formula mixing them is a guess.
Last step, the quota. The API keeps the three best platform passages and the three best upstream passages. Those numbers are a starting point that Part 2 will tune.
Why a quota, when fusion already ranks everything? Because I tried without. On a test corpus of twenty passages, a vector search for our running question returns three upstream passages first. They all talk about kafka-topics.sh. The Aiven passage that answers the question for an Aiven user is not in the top three. Adding keyword search and fusion did not change that: the platform passage stayed third. It only moved up when the search was filtered by source.
That gave me the sentence I would keep from this whole article: hybrid search improves relevance, and only the quota settles authority. A better ranking algorithm cannot know that for this user, on this platform, one correct document outranks another correct document. You have to tell it.
The prompt finishes the job. Platform passages go first, every passage is labelled with its source, and the instruction is explicit: for a service running on Aiven, when the passages disagree, the platform passage wins, and the answer must say that the upstream method exists and does not apply here.
Two models, two different deals
The platform uses two models, and they are not handled the same way.
The encoder is ours. It is BAAI/bge-small-en-v1.5: 384 dimensions, about 130 MB, and it runs on CPU through ONNX. Aiven Runtime has no GPU today and this model does not need one. It sits in two places: inside the worker for indexing, and inside the API for questions. It has to be the same model on both sides. Vectors from two different encoders are not comparable, and the search would return nonsense without raising any error. So every chunk stores its model_version, and the API refuses to start if the version in the index is different from its own.
One constraint comes with this model: it reads at most 512 tokens and silently drops the rest. The chunk size is therefore counted with the encoder's tokenizer, never the decoder's.
The decoder is rented. Generation goes through the AI Gateway. We send a prompt and receive tokens. No weights to store, no GPU to find, and changing model is a configuration value.
The big model is rented and the small one is owned. I find that split healthy. The part that defines your index, and that would force a full re-indexing if it changed under you, stays under your control. The part that is expensive to run and improves every few months stays replaceable.
The cache, and the trap I walked into
My first plan was a semantic cache. Embed the incoming question, look for a similar one already answered, and return that answer without paying for a generation. Valkey 9 has vector search, so the feature was right there. I tested it.
With "How do I add partitions to a Kafka topic?" in the cache, I sent three new questions.
"How can I increase the number of partitions of an existing topic?" is a true paraphrase. Its distance to the cached question is 0.145. It should be served from the cache.
"How do I remove partitions from a Kafka topic?" comes back at 0.122. Closer than the paraphrase. And the answer is the opposite: Kafka cannot reduce the partition count of a topic. A cache hit here tells the user how to do something impossible.
Set the threshold at 0.15 and you serve both. Set it at 0.10 and you serve neither. No value separates them, because an embedding measures what a sentence is about, and "add" versus "remove" is one word in a sentence that is otherwise identical.
So Valkey keeps a plain cache. The key is the normalised question plus the version of the model and of the corpus, so a re-indexing empties it naturally. Fewer hits, and none of them wrong.
Data contracts
Four topics carry everything.
| Topic | Key | One event is | Written by |
|---|---|---|---|
documents.raw | doc_id | one documentation page, as text | fetcher |
chunks.embedded | chunk_id | one chunk with its vector | chunk-and-embed worker |
inference.results | request_id | one answered question | API |
eval.scores | request_id | one scored answer | eval runner |
A chunk. This is the event that both sinks consume, so it is the contract that matters most.
Loading code...
Three fields deserve a comment. token_count comes with the name of its tokenizer, because encoder and decoder count the same text differently. content_hash lets the fetcher skip pages that did not change. And the vector travels as text, which brings us to PostgreSQL.
The PostgreSQL table. The JDBC sink knows integers and strings. It does not know the vector type. Rather than fight the connector, let the database do the conversion with a generated column:
Loading code...
The sink writes embedding_txt, PostgreSQL fills embedding. In the test, every row came out with 384 dimensions and a norm of 1.000. The sink runs in upsert mode on chunk_id: I replayed a modified chunk, the table kept the same number of rows, and the vector was recomputed. One index per authority, since every search is filtered on it.
The OpenSearch index. One rule: create the index before the connector. If the connector creates it, the mapping is guessed, and the embedding becomes an ordinary array of floats that no k-NN query can use.
Loading code...
The index has the name of the topic, and the message key becomes the document id, so a replay overwrites documents and never duplicates them. Our retriever only uses BM25 here. The vector is indexed too because it costs little and it is what allows the comparison in Part 2. I checked that the k-NN scores in OpenSearch match the cosine values in PostgreSQL to the third decimal, in the same order. Same data, two engines, same answer: the comparison will be fair.
A trace event. One per answer, written by the API after the last token.
Loading code...
The zeros are placeholders. Part 2 fills them with measurements. conflict is true when the retained passages come from both sources for the same product, and authority_used records which side the answer followed. Those two fields give the dashboard its most useful panel: how often the sources disagree, and what the assistant did about it. lang is there to learn which languages users really write in before deciding anything about multilingual support.
In ClickHouse, a table with the Kafka engine reads the topic and a materialised view copies the rows into a ReplicatedMergeTree table, ordered by time. A second view keeps per-minute aggregates for the dashboards.
One caveat on formats. The tests ran with JSON messages carrying their own schema, which the JDBC sink requires in that mode. The target is schemas registered in the Schema Registry. Part 2 makes that switch and confirms that both sinks still behave.
What I ruled out
A semantic answer cache. Measured, and unsafe. See above.
Fine-tuning the decoder. The assistant needs facts that change with every release of the docs. Retrieval updates in minutes when a page changes. A fine-tuned model would need a new training run, and it still could not cite its source.
A language router in front of the encoder. The corpus is in English and so is the series. If the traces show a real share of other languages, the fix is a multilingual encoder and a re-index, which the topics make cheap.
Running everything inside OpenSearch. It is a valid design and maybe the right one for you. It stays in the series as the baseline to beat.
Asynchronous generation through a topic. An earlier draft queued requests in Kafka and had a worker call the model. With a hosted endpoint that streams tokens, the API calls it directly. Kafka stays for what happens before and after the answer.
On to Part 2
The design is on paper and its risky joints are tested. What is missing is everything you only learn by running it: how many chunks per second the encoder processes on a small Runtime plan, how long a question takes at each step, whether three plus three is the right quota, and whether two engines beat one.
Part 2 deploys the whole thing, step by step, and puts a number next to each of those questions.
Get building: Start free on Aiven




