Getting Started with Development of Forum-Based Web using Redis (Part 1)
Tags: redis, beginners, webdev, tutorial
Published: 2022-07-17 00:04:38

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41r20xdipjqrsp4bdenq.jpeg)

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](https://redis.io/docs/stack/) for the development process, but you might need [Redis Enterprise](https://redis.com/redis-enterprise/advantages/) 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:

* [Shared Database Explanation from microservices.io by Chris Richardson](https://microservices.io/patterns/data/shared-database.html)
* [Shared Database Explanation from AWS](https://docs.aws.amazon.com/prescriptive-guidance/latest/modernization-data-persistence/shared-database.html)
* [Shared Database Explanation from baeldung.com](https://www.baeldung.com/cs/microservices-db-design#shared-db)

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](https://microservices.io/patterns/data/database-per-service.html) 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](https://docs.docker.com/get-docker/) for more information about it. Please make sure the Docker Compose is also within the installation. Please visit [the installation page](https://docs.docker.com/compose/install/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aj0dva3vl5czt9zsjr0n.png)

    ![Verify Docker Compose Installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sal7oreem4ugd3a3rj94.png)
 
2. Install .NET SDK. I use .NET 6. We will use .NET for `User Service` and `Thread Service`. Please visit [this page](https://dotnet.microsoft.com/en-us/download) for the installer and the guide. Please verify the installation using `dotnet --info` command.

    ![Verify .NET](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzm7sq9c6cgaga4h34r0.png)

3. Install Node.js and yarn. Check this page for .Node.js installer. I use node version `18.3.0`. Check [this page](https://classic.yarnpkg.com/en/docs/install) for yarn installer. I still use version 1 of yarn. Please verify your installation with `node --version` and `yarn --version`.

    ![Node version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhtglr6l6n2u6hz3kw6d.png)

    ![Yarn Version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jo4mn2csf8qvgg0dpy9u.png)

### Project Structure Preparation <a name="project-stucture"></a>

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

### User Service <a name="user-service"></a>

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

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

![Project Build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fh43nkiedv3ejk7h64u3.png)
 
### 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!

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

    ![init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qp1lw0uf6rv6qzajr2a.png)

2. Install all initial dependencies.

    ```bash
    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
    ```

3. Initiate `tsconfig.json` file. Using this command: `yarn tsc --init`. Override the config like this.

    ```json
    {
      "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. */
      }
    }
    ```

4. Initiate our project using this file and store it with name `index.ts` and store it at `src` folder.

    ```js
    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}`)
    });
    ```

5. Please make sure your `.eslintrc.json` file like this.

    ```json
    {
      "parser": "@typescript-eslint/parser",
      "plugins": ["@typescript-eslint"],
      "extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/recommended"
     ]
    }
    ```

6. Add some scripts runner and modify other configs in your `package.json`.

    ```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](#user-service).

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

> You can check the final result will be like [this](https://github.com/bervProject/forum-api-microservices/tree/step-1).

## Repository

{% github bervProject/forum-api-microservices %}

## What's Next?

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

![Thanks GIF](https://media.giphy.com/media/ZfK4cXKJTTay1Ava29/giphy.gif)