All eyes on AI: 2026 predictions – The shifts that will shape your stack.

Read now

Tutorial

How to build a HackerNews Clone using Redis

February 26, 20269 minute read
Ajeet Raina
Ajeet Raina
TL;DR:
Build a Hacker News clone with Redis, use Next.js and React for the frontend and Node.js with Express for the backend. Store user profiles, posts, and comments as Redis JSON documents, index them with FT.CREATE for full-text search, and use sorted sets to rank content by score. The result is a fast, full-stack social news app with voting, comments, user authentication, and moderation — all powered by Redis.
Hacker News (sometimes abbreviated as HN) is a social news website focusing on computer science and entrepreneurship. It developed as a project of Graham's company Y Combinator, functioning as a real-world application of the Arc programming language which Graham co-developed.
This is a HackerNews clone built upon React, NextJS as a frontend and NodeJS, ExpressJS & Redis as a backend. This application uses JSON for storing and searching the data in Redis.
Full-stack Hacker News clone built with Next.js and Redis showing the front page with ranked posts

#What will you learn?

  • How to model users, posts, and comments as Redis JSON documents
  • How to create search indexes with FT.CREATE and query them with FT.SEARCH
  • How to implement a voting and ranking system using Redis sorted sets
  • How to build user authentication with hashed passwords and auth tokens
  • How to wire up a Next.js frontend to a Redis-backed Express API

#What will you build?

A fully functional Hacker News clone that supports:
  • User registration and login
  • Submitting, upvoting, and commenting on posts
  • Real-time content ranking by score
  • Full-text search across post titles
  • A moderation log for admin actions

#What are the prerequisites?

Before getting started, make sure you have the following installed:
If you are new to Node.js and Redis, start with the Getting Started with Node.js and Redis tutorial first.

#How do you create the Redis Cloud database?

Redis Cloud is a fully-managed cloud service for hosting and running your Redis dataset in a highly-available and scalable manner, with predictable and stable top performance. You can quickly and easily get your apps up and running with Redis Cloud — just tell us how much memory you need and get started instantly with your first Redis database. You can then add more Redis databases (each running in a dedicated process, in a non-blocking manner) and increase or decrease the memory size of your plan without affecting your existing data.
Follow this link to create a Redis Cloud account with 2 databases.
Save the database endpoint URL and password for future reference.

#How do you clone and configure the project?

#Clone the repository

#Set up environment variables

Copy .env.sample to .env and provide the values as shown below:

#How do you run the application?

#Start the development server

#Seed the database with Hacker News data

Using the Hacker News API, the seed script pulls the latest data. First create a moderator with moderator:password123, then run:

#Access the application

Open https://localhost:3001 and you should see the HackerNews login screen:
Hacker News clone login page with sign-up form for creating a new account

#How does each screen work?

#How does user signup work?

Hacker News clone sign-up form showing username and password fields
  • Make sure user(where username is andy1) does not exist.
  • Get and increase the next id in users collection.
  • Create user:63 hash and json.(json also collects authToken and password hash etc)

#How does user login work?

Hacker News clone login form with username and password fields for returning users
  • Find user
  • Make sure password is correct
  • Compare password and new password hash and create cookie if it's successful

#How does the item list page work?

Hacker News clone front page showing ranked list of submitted news items with vote counts
  • Check if user has toggled hidden attribute on a specific item.
  • If that is not null
  • If it's empty array
  • Get all items from Redis using JSON.MGET
  • Get items posted within last 1 week
NOTE
In this case, 1615652598 is a timestamp of 1 week ealier than current timestamp

#How does the item detail page work?

Hacker News clone item detail view showing post content with nested comment thread
  • Get the item object first
  • Find item:1 's root comments
  • Get those comments
  • Using children of each comment, fetch children comments
  • Iterate this over until all comments are resolved

#How does submitting a new item work?

Hacker News clone submission form with fields for title, URL, and text content
  • Get next item's id and increase it
  • Create hash and index

#How does updating a user profile work?

Hacker News clone user profile settings page with editable about, email, and preferences fields
  • Get the user
  • Update new user

#How do moderation logs work?

Hacker News clone moderation log page showing timestamped admin actions on posts and users
  • Find all moderation logs
  • Get that moderation logs

#How does search work?

Hacker News clone search results page showing posts matching the query "fa"
  • Get items that contains "fa"
  • Get those items via json

#What Redis commands does this application use?

#There are 2 type of fields, indexed and non-indexed

  1. Indexed fields will be stored in hash using HSET/HGET.
  2. Non-indexed fields will be stored in JSON.
  • Create an index
When schema is created, it should created index.
  • Drop search index
Should drop/update index if the schema has changed
  • Get search info
Validate if the fields are indexed properly. If not, it will update the index fields or drop/recreate.
  • Create a new user
It will require new hash and new JSON record
  • Update a user
  • Find user with username 'andy'
  1. Find the user's hash first
2. Fetch the JSON object to get the related JSON object
  • Find user whose id is andy1 or andy2
  • Find user whose id is not andy1 or andy2
  • Find user whose id is andy1 or username is andy
  • Find user whose id is andy1 and username is andy
  • Find first 10 users order by username
  • Find next 10 users
  • Get from JSON from multiple keys

#Next steps

Now that you've built a Hacker News clone with Redis, here are some ways to keep going:

#References