Patterns

This document outlines the core architectural patterns implemented in GitLab Discussions.

Microservices

The GitLab Discussions application is built as a separate microservice within the GitLab ecosystem.

https://about.gitlab.com/handbook/engineering/architecture/

Why microservices?

  • Scalability: Individual services can be scaled independently to handle different workloads.
  • Independent Deployment: Each microservice can be deployed and updated without affecting other parts of the system.
  • Fault Isolation: Issues in one microservice are less likely to affect the entire system.

MVC (Model-View-Controller)

The GitLab Discussions microservice follows the MVC architectural pattern for organizing code.

Model: Represents the data structures and business logic.

  • Example: app/models/discussion.rb

View: Responsible for presenting the data to users.

  • Example: app/views/discussions/index.html.haml

Controller: Handles user interactions and requests.

  • Example: app/controllers/discussions_controller.rb

RESTful API

The GitLab Discussions service exposes a RESTful API for interacting with discussions. This allows for integration with other GitLab features and external applications.

https://docs.gitlab.com/ee/api/discussions.html

Example Requests:

  • GET /projects/:id/discussions - Retrieves a list of discussions for a project
  • POST /projects/:id/discussions - Creates a new discussion
  • PUT /projects/:id/discussions/:discussion_id - Updates an existing discussion
  • DELETE /projects/:id/discussions/:discussion_id - Deletes a discussion

GraphQL

The GitLab Discussions service also offers a GraphQL API for more complex queries and mutations.

https://docs.gitlab.com/ee/api/graphql.html

Example Queries:

  • discussions(projectId: 100, state: “open”) { … } - Retrieves a list of open discussions for a project

Authentication and Authorization

The GitLab Discussions service integrates with the GitLab authentication and authorization system, ensuring secure access to resources. Users are authenticated using their GitLab credentials, and authorization is enforced based on roles and permissions.

https://docs.gitlab.com/ee/api/README.html#authentication

Example:

# Example code demonstrating authorization
          if current_user.can?(:read_discussion, discussion)
            # Allow access to the discussion
          end
          

Database

The GitLab Discussions service leverages PostgreSQL as its primary database.

https://docs.gitlab.com/ee/administration/database/postgresql.html

Example:

# Example code interacting with the database
          discussion = Discussion.find(1)
          

Caching

The GitLab Discussions service uses caching to improve performance by storing frequently accessed data in memory.

Example:

# Example code using caching
          Rails.cache.fetch("discussion_count", expires_in: 1.hour) do
            Discussion.count
          end
          

Logging

The GitLab Discussions service uses logging to record events and debug issues. Logs are stored in a centralized logging system for analysis and troubleshooting.

https://docs.gitlab.com/ee/administration/monitoring/logging/

Example:

# Example code logging an error
          Rails.logger.error("Error processing discussion: #{exception.message}")