Tutorial
How to build a HackerNews Clone using Redis
February 26, 20269 minute read
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 withFT.CREATEfor 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.

#What will you learn?
- How to model users, posts, and comments as Redis JSON documents
- How to create search indexes with
FT.CREATEand query them withFT.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:
- Node.js v15.10.0 or later
- NPM v7.8.0 or later
- A Redis Cloud account (free tier works)
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:

#How does each screen work?
#How does user signup work?

- 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?

- 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?

- 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
NOTEIn this case, 1615652598 is a timestamp of 1 week ealier than current timestamp
#How does the item detail page work?

- 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?

- Get next item's id and increase it
- Create hash and index
#How does updating a user profile work?

- Get the user
- Update new user
#How do moderation logs work?

- Find all moderation logs
- Get that moderation logs
#How does search work?

- 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
- Indexed fields will be stored in hash using HSET/HGET.
- 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'
- 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:
- Build a social network app: Follow the How to Build a Social Network Application using Redis and NodeJS tutorial to explore user matching with search in Redis.
- Add a shopping cart: See How to Build a Shopping Cart App Using Node.js and Redis for another full-stack Redis project.
- Learn Redis fundamentals: Check out the Redis quick start to deepen your understanding of core commands and data structures.
- Explore Redis JSON: Read more about JSON in Redis and how to index and query JSON documents.
- Try Redis Cloud: Sign up for a free Redis Cloud account to deploy your app with a managed Redis instance.
#References
- Learn more about JSON in Redis in the Redis quick start tutorial.
- How to build shopping cart app using NodeJS and JSON in Redis
- Indexing, Querying, and Full-Text Search of JSON Documents with Redis
