Category: system_design Date: 2026-03-30
System Design Discussion: Design Search Autocomplete
Functional Requirements:
Non-functional Requirements:
The Search Autocomplete system consists of:
We will use a combination of a relational database (RDBMS) and a NoSQL database:
Table Design:
items table:
id (primary key)namedescriptioncategoryuser_queries table:
id (primary key)query (user input)timestampautocomplete_suggestions collection:
suggestion (autocomplete suggestion)count (frequency of suggestion)To handle a large number of concurrent users and search queries:
Potential bottlenecks:
Trade-offs:
First Principle of System Design:
The first principle of system design is to “Separate Concerns”. In this case, we separate the autocomplete logic from the search logic by implementing a caching layer and a separate database for storing autocomplete suggestions. This allows us to handle the autocomplete logic in real-time, while the search logic can be handled by the database.
Learning Links:
Example Code (Node.js and MongoDB):
// Node.js and MongoDB example
const express = require('express');
const app = express();
const mongoose = require('mongoose');
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/searchautocomplete', { useNewUrlParser: true, useUnifiedTopology: true });
// Define schema for autocomplete suggestions
const suggestionSchema = new mongoose.Schema({
suggestion: String,
count: Number
});
// Create collection for autocomplete suggestions
const Suggestion = mongoose.model('Suggestion', suggestionSchema);
// Handle autocomplete requests
app.get('/autocomplete', (req, res) => {
// Get user input
const query = req.query.query;
// Get autocomplete suggestions from cache or database
Suggestion.find({ suggestion: { $regex: query, $options: 'i' } }, (err, suggestions) => {
if (err) {
res.status(500).send({ message: 'Error fetching autocomplete suggestions' });
} else {
res.send(suggestions);
}
});
});
// Listen on port 3000
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
This is a basic example of how to design a Search Autocomplete system using Node.js and MongoDB. In a real-world scenario, you would need to handle more complex requirements and edge cases.