New from O’Reilly: The memory architecture behind adaptive AI agents

Read the report
For developersHow to build a HackerNews Clone using Redis
Ajeet Raina
Ajeet Raina

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 the data and Search in Redis Stack for searching.

Image

Step 1. Install the prerequisites

Install the below packages

  • NPM v7.8.0
  • NODE v15.10.0

Step 2. Create Redis Cloud database

Redis is an open source, in-memory, key-value data store most commonly used as a primary database, cache, message broker, and queue. Redis is popular among the developers as it delivers sub-millisecond response times, enabling fast and powerful real-time applications in industries such as gaming, fintech, ad-tech, social media, healthcare, and IoT.

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. Redis Cloud allows you to run Redis server over the Cloud and access instance via multiple ways like RedisInsight, Redis command line as well as client tools. You can quickly and easily get your apps up and running with Redis Cloud through its Redis Heroku addons , 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 with Redis Stack.

Save the database endpoint URL and password for our future reference

Step 3. Clone the repository

Step 4. Setting up environment variables

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

Step 5. Run the developer environment

Step 6. Pull Hacker News API to seed database

Using API, it pulls the latest hackernews data. Next, you need to seed top stories from hacker news. First create a moderator with moderator:password123

Step 7. Access the HackerNews URL

Open https://localhost:3001 and you should be able to access the HackerNews login screen as shown below:

Image

How it works

By Screens

Signup

Image

  • 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)

Login

Image

  • Find user
  • Make sure password is correct
  • Compare password and new password hash and create cookie if it's successful

Item list page

Image

  • 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

Item Detail

Image

  • 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

Submit

Image

  • Get next item's id and increase it
  • Create hash and index

Update Profile

Image

  • Get the user
  • Update new user

Moderation Logs screen

Image

  • Find all moderation logs
  • Get that moderation logs

Search

Image

  • Get items that contains "fa"
  • Get those items via json

Example commands

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

References