DEV Community

Cover image for Getting Started with Development of Forum-Based Web using Redis (Part 1)
Bervianto Leo Pratama
Bervianto Leo Pratama

Posted on

Getting Started with Development of Forum-Based Web using Redis (Part 1)

Hello everyone!

I will give you tutorials to build a Forum-Based Website using Redis. We will have the software architecture like this picture.

Software Architecture

In short, we will have MongoDB for persistent data and utilize Redis as a cache, but not only using it as a cache server. We will explore more about Redis's capabilities. We will use Redis Stack for the development process, but you might need Redis Enterprise for production use.

Let's get started!

Setup Project

We will use a microservices structure but with the same database for each service. This approach is called the shared-database-per-service pattern. If you want to know more about the shared-database-per-service pattern, please visit these links:

Why did I choose the shared-database-per-service pattern? I didn't want this tutorial to have more complex. I want to introduce microservices but as simple as it does. We can refactor our code to use the database per service pattern for later. It's quite common when we will migrate from monolith to microservices using the shared-database-per-service pattern.

Tools Preparation

  1. Please download/setup Docker. Please visit this page for more information about it. Please make sure the Docker Compose is also within the installation. Please visit the installation page if you don't have it yet. Verify the installation of Docker using docker version command. Otherwise, for Docker Compose using docker compose version command. We will host MongoDB and Redis using Docker.

    Verify Docker Installation

    Verify Docker Compose Installation

  2. Install .NET SDK. I use .NET 6. We will use .NET for User Service and Thread Service. Please visit this page for the installer and the guide. Please verify the installation using dotnet --info command.

    Verify .NET

  3. Install Node.js and yarn. Check this page for .Node.js installer. I use node version 18.3.0. Check this page for yarn installer. I still use version 1 of yarn. Please verify your installation with node --version and yarn --version.

    Node version

    Yarn Version

Project Structure Preparation

We will use this project structure.

forum-api-microservices/
├── app/
│   ├── auth-service/
│   │   ├── src/
│   │   │   └── index.ts
│   │   ├── .eslintignore
│   │   ├── .eslintrc.json
│   │   ├── .gitignore
│   │   ├── Dockerfile
│   │   ├── package.json
│   │   ├── tsconfig.json
│   │   └── yarn.lock
│   ├── thread-service/
│   │   ├── ThreadService/
│   │   │   ├── appsettings.json
│   │   │   ├── Program.cs
│   │   │   └── ThreadService.csproj
│   │   ├── .editorconfig
│   │   ├── .gitignore
│   │   ├── Dockerfile
│   │   └── thread-service.sln
│   └── user-service/
│       ├── UserService/
│       │   ├── appsettings.json
│       │   ├── Program.cs
│       │   └── UserService.csproj
│       ├── .editorconfig
│       ├── .gitignore
│       ├── Dockerfile
│       └── user-service.sln
├── docker-compose.yml
├── LICENSE
└── README.md
Enter fullscreen mode Exit fullscreen mode

User Service

We will use .NET 6 for User Service Project. Please follow this step by step.

  1. Please create directory app/user-service and run all the commands below in that directory.
  2. Generate Project using this command dotnet new webapi -minimal -o UserService.
  3. Generate .gitignore file. Using this command: dotnet new gitignore.
  4. Generate .sln file. Using this command: dotnet new sln.
  5. Add our .csproj or project to the solution file. Using this command: dotnet sln add UserService.
  6. Verify your project structure. Your project structure should be like the project stucture section.
  7. Please check your project completeness by building the project. Run this command: dotnet build.

Project Build

Auth Service

We will use Node.js as the main language for Auth Service. Why didn't we use the same language? These are the special things when we use the microservices approach. We can use different languages for different services. Let's go!

  1. Please make sure you've created the app/auth-service directory and we will run all the commands below in that directory.
  2. Initiate package.json file using yarn init. Fill in all the information that you need.

    init

  3. Install all initial dependencies.

    yarn add express@^4.18.1 # install main dependency
    yarn add --dev typescript@^4.7.4 ts-node-dev@^2.0.0 @types/express@^4.17.13 @types/node@^14.11.2 # install main development tools
    yarn add --dev eslint@^8.18.0 @typescript-eslint/eslint-plugin@^5.29.0 @typescript-eslint/parser@^5.29.0 # install linter
    
  4. Initiate tsconfig.json file. Using this command: yarn tsc --init. Override the config like this.

    {
      "compilerOptions": {
        /* Visit https://aka.ms/tsconfig to read more about this file */
       "target": "es2017",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
        "module": "node16",                                /* Specify what module code is generated. */
        "moduleResolution": "node",                       /* Specify how TypeScript looks up a file from a given module specifier. */
        "outDir": "./lib",                                   /* Specify an output folder for all emitted files. */
        "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
        "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
        "strict": true,                                      /* Enable all strict type-checking options. */
        "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
      }
    }
    
  5. Initiate our project using this file and store it with name index.ts and store it at src folder.

    import express from 'express';
    
    const port = parseInt(process.env.PORT || '9000');
    const app = express();
    app.use(express.json());
    
    app.get("/", (req, res) => {
        res.json({
            message: 'Hello World!',
        });
    })
    
    app.listen(port, () => {
        console.log(`Server listen on port ${port}`)
    });
    
  6. Please make sure your .eslintrc.json file like this.

    {
      "parser": "@typescript-eslint/parser",
      "plugins": ["@typescript-eslint"],
      "extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/recommended"
     ]
    }
    
  7. Add some scripts runner and modify other configs in your package.json.

    // ... other configs
    "main": "lib/index.js",
    "scripts": {
      "start": "node lib/index.js",
      "start:dev": "ts-node-dev src/index.ts",
      "compile": "tsc",
      "lint": "eslint .",
      "fix": "eslint . --fix"
    },
    // ... other configs
    

Thread Service

The step by step of Thread Service will same with User Service.

  1. Please create directory app/thread-service and run all the commands below in that directory.
  2. Generate Project using this command dotnet new webapi -minimal -o ThreadService.
  3. Generate .gitignore file. Using this command: dotnet new gitignore.
  4. Generate .sln file. Using this command: dotnet new sln.
  5. Add our .csproj or project to the solution file. Using this command: dotnet sln add ThreadService.
  6. Verify your project structure. Your project structure should be like the project stucture section.
  7. Please check your project completeness by building the project. Run this command: dotnet build.

You can check the final result will be like this.

Repository

forum-api-microservices

Forum API Microservices

Directory Structure

Microservices Development

  • You will need to copy or modify docker-compose.yml to ignore the deployment of microservices.
  • Run Redis & MongoDB using docker compose up -d.
  • Go to the microservice you want to update and read the README.md of each directory to understand how to run them.

Development

  • Build Images of Microservices: docker compose build
  • Run all: docker compose up -d

Software Architecture

Software Architecture

License

MIT

MIT License
Copyright (c) 2022 Bervianto Leo Pratama's Personal Projects

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute,

What's Next?

We will continue to build User Service in the next part. So please navigate to the next part.

Thanks GIF

Top comments (0)