This project demonstrates a realtime search box for users to search articles, with an analytics feature that displays what users are searching for. The goal is to record user input in realtime and ultimately display analytics and trends on the most popular searches.
- Realtime Search Logging: Captures and records user searches in realtime.
- Analytics: Displays trends and analytics on what users are searching for.
- Scalability: Designed to handle thousands of requests per hour.
- User Isolation: Each user's search data is kept separate.
- Rspec Tests: Includes tests to ensure the robustness of the application.
- User Input Capture: As a user types into the search box, each keystroke triggers a JavaScript event that sends the current search term to the server via an POST request.
- Debounce Mechanism: A debounce function is implemented to limit the number of requests sent to the server, reducing unnecessary load and preventing excessive logging of incomplete search terms.
- Temporary Storage in Redis: Each partial search term is stored in a Redis list unique to the user. Redis is chosen for its high performance and ability to handle a large volume of operations per second.
- Delayed Processing: After a short delay, a Sidekiq job (
SearchProcessorJob
) is enqueued to process the search terms stored in Redis. - Consolidation of Search Terms: The
SearchProcessorJob
retrieves the list of search terms from Redis, discards incomplete terms, and saves only the final, complete search term to the database. This step ensures that only meaningful search queries are logged, avoiding the pyramid problem where intermediate, incomplete searches clutter the data.
- Search Data Aggregation: The saved search terms are periodically aggregated to generate analytics, providing insights into what users are searching for most frequently. This data is accessible on the
Admin Panel
route/admin
.
By combining Redis for fast, temporary storage and Sidekiq for efficient, reliable background processing, we achieve a scalable and responsive system for capturing and analyzing real-time search data.
- Ruby 3.1.4+
- Rails 7.0.8+
- Redis
- Sidekiq
- Clone the Repository:
git clone https://github.com/lemachegabriel/RealtimeSearchAnalytics cd RealtimeSearchAnalytics
- Install Dependencies:
bundle install
- Set up the database and precompile:
rails db:create rails db:migrate rails assets:precompile
- Start Redis:
redis-server
- Start Sidekiq:
bundle exec sidekiq
- Start the Rails server:
rails s
RSpec is used for testing the application. To run the tests, use:
bundle exec rspec