I was building a search feature for a client’s large product catalog, and the usual solutions felt heavy. The server was fast, but the search layer was a bottleneck. That’s when I started looking at a different approach. What if we could pair a lightning-fast web framework with a search engine built for speed from the ground up? This led me to combine Fastify and Typesense. The result wasn’t just an improvement; it felt like a new category of performance. Let me show you how this works.
Fastify is a web framework for Node.js that focuses on speed and low overhead. It handles requests incredibly quickly. Typesense is an open-source search engine. It’s designed to give you instant, typo-tolerant search results without the complexity of larger systems. When you put them together, you get an application that is fast at every level. The server responds quickly, and the search results come back almost instantly.
Why does this pairing make sense? Both tools value performance and a good developer experience. Fastify has a clean plugin system and excellent TypeScript support. Typesense has a simple API and is easy to run. They complement each other. You get a streamlined stack that is powerful yet simple to manage. This is perfect for features where search is not just an add-on, but a core part of the application.
Setting this up is straightforward. First, you need to install the necessary packages. You’ll need fastify and the Typesense client library.
npm install fastify typesense
Next, you create your Fastify server and connect to Typesense. The idea is to create the Typesense client and make it available throughout your Fastify application. We do this by using a decorator.
const fastify = require('fastify')({ logger: true });
const Typesense = require('typesense');
const typesenseClient = new Typesense.Client({
nodes: [{
host: 'localhost',
port: '8108',
protocol: 'http'
}],
apiKey: 'your-search-only-api-key',
connectionTimeoutSeconds: 2
});
// Decorate the Fastify instance with the Typesense client
fastify.decorate('typesense', typesenseClient);
Now, any part of your Fastify application can access the search client via fastify.typesense. This keeps your code organized. Have you ever struggled with where to put your database clients? This pattern provides a clean, official place for it.
Let’s create a simple search route. Imagine we have indexed a collection of books in Typesense. We can create a Fastify route to query that collection.
fastify.get('/search', async (request, reply) => {
const searchParameters = {
q: request.query.q,
query_by: 'title,author',
per_page: 10
};
try {
const searchResults = await fastify.typesense
.collections('books')
.documents()
.search(searchParameters);
return { results: searchResults };
} catch (error) {
fastify.log.error(error);
reply.code(500).send({ error: 'Search failed' });
}
});
With just a few lines, we have a high-performance search endpoint. The request comes into Fastify’s optimized router and is passed to the Typesense client, which communicates with the search engine using its efficient protocol. The speed is noticeable.
But what about the data? How do you get it into Typesense in the first place? You need to create a schema and index your documents. This is often a one-time setup or a process run when data changes. Here’s how you might define a schema for our book example and index a document.
// Define the schema for your collection
const bookSchema = {
name: 'books',
fields: [
{ name: 'title', type: 'string' },
{ name: 'author', type: 'string' },
{ name: 'publication_year', type: 'int32' },
{ name: 'categories', type: 'string[]' }
],
default_sorting_field: 'publication_year'
};
// Create the collection (usually done in a setup script)
await fastify.typesense.collections().create(bookSchema);
// Index a single document
const document = {
title: 'The Great Gatsby',
author: 'F. Scott Fitzgerald',
publication_year: 1925,
categories: ['fiction', 'classic']
};
await fastify.typesense
.collections('books')
.documents()
.create(document);
This integration truly shines in real applications. Consider an online store. A user types “laptp” into the search bar. With Typesense’s typo tolerance, they still see results for “laptop.” Fastify ensures the request for these results is processed without delay. The combination delivers a seamless experience. What do you think the impact of a half-second delay in search is on user engagement? Studies suggest it’s significant.
For content-heavy sites like documentation or blogs, this stack is a game-changer. You can index every article, tutorial, and code snippet. When a developer searches for “authentication middleware,” they get relevant results instantly. The speed encourages exploration and helps users find answers before frustration sets in.
The architectural benefits are clear. Both Fastify and Typesense are lightweight. They don’t demand huge amounts of memory or CPU. This means you can run a performant search application on cost-effective infrastructure. As your user base grows, you can scale each component independently. Fastify can handle more HTTP traffic, while you can add more nodes to your Typesense cluster for search throughput.
Building with these tools has changed how I think about application performance. It proves that you don’t need the most complex system to get top-tier results. Sometimes, the best solution comes from combining focused, efficient technologies. The synergy between Fastify’s request handling and Typesense’s search algorithms creates something greater than the sum of its parts.
I encourage you to try this setup. Start with a small project. Index a few hundred documents and feel the response time. The developer experience is as smooth as the user experience. If you’ve been dealing with slow search or bloated backends, this might be the refresh you need. Did this approach spark an idea for your current project?
If you found this walk-through helpful, please share it with other developers who might be battling performance issues. Have you used Fastify or Typesense before? What was your experience? Let me know in the comments below—I’d love to hear about your projects and answer any questions you have.
As a best-selling author, I invite you to explore my books on Amazon. Don’t forget to follow me on Medium and show your support. Thank you! Your support means the world!
101 Books
101 Books is an AI-driven publishing company co-founded by author Aarav Joshi. By leveraging advanced AI technology, we keep our publishing costs incredibly low—some books are priced as low as $4—making quality knowledge accessible to everyone.
Check out our book Golang Clean Code available on Amazon.
Stay tuned for updates and exciting news. When shopping for books, search for Aarav Joshi to find more of our titles. Use the provided link to enjoy special discounts!
📘 Checkout my latest ebook for free on my channel!
Be sure to like, share, comment, and subscribe to the channel!
Our Creations
Be sure to check out our creations:
Investor Central | Investor Central Spanish | Investor Central German | Smart Living | Epochs & Echoes | Puzzling Mysteries | Hindutva | Elite Dev | JS Schools
We are on Medium
Tech Koala Insights | Epochs & Echoes World | Investor Central Medium | Puzzling Mysteries Medium | Science & Epochs Medium | Modern Hindutva