/go_psql_redis_example

This is an example of a simple microservice written in go use Postgres as database and Redis as cache.

Primary LanguageGoMIT LicenseMIT

Go Release

Caching in Go: Redis

This is an example of a simple microservice written in go. Microservice gives us the requested ID from the user database. After getting from the database, the query result is cached in Redis for 25 seconds. For logging, a zap is used, a gorilla / mux is selected as a router. For stack trace of errors use Sentry.

This example starts a http server on port 8080 at any available ip address.

Supported environment variables:

Set APP_LOG_FILE=file_name environment variable for enable output logs in project dir to file_name.

Set APP_LOG_LEVEL=DEBUG|ERROR|WARNING|INFO environment variable to change log level

Set JAEGER_AGENT_HOST=localhost environment variable for chose Jaeger ip address

Set JAEGER_AGENT_HOST=6831 environment variable for chose Jaeger port

Set DATABASE_URL=postgres://rexamp:password@pgbouncer:6432/redisexamp?sslmode=disable environment variable for DB

Set REDIS=redis:6379 environment variable for Redis

Set SENTRY_DSN=your_sentry_dsn environment variable for Sentry Tracing

Requirements

  1. Go 1.17
  2. PostgreSQL: either a remote instance, local binary or docker container.
  3. Redis: either a remote instance, local binary or docker container.
  4. Jaeger: either a remote instance, local binary or docker container.
  5. Load testing data: included in this repo
  6. Benchmark tests: use a gobench tool
  7. Prometheus Metrics: about collected metrics
  8. Grafana Dashboards: recommended for use with this stack
  9. Kubernetes Probes: app k8s probes

Running

For the convenience of using all possible commands, a makefile has been prepared.

Download the required packages:

$ make deps

Then you can run this example with the PostgreSQL and Redis and importing testing data:

$ make run

PostgreSQL

This example Using Docker Container with Postgres 12

Good article from the report on how to work with Postgres from GO (rus).

An article about the speed of work of requests from the Postgres database with an increase in connections to it more than 100 (eng).

$ make postgres

Redis

This example Using Docker Container with Redis 6

$ make redis

Jaeger

This example Using Docker Container with Jaeger

$ make jaeger

Load testing data

This repository contains test data in the sql folder to demonstrate how the example works. The data was taken from here.

$ make initdb

Benchmark tests

The gobench tool is used for load testing.

First install tool by command:

$ make bench-install

Then you can use it with command:

$ make stress

Prometheus Metrics

For run and use ELK, Prometheus and Grafana run stack via docker-compose, before run make .env file with content:

DATABASE_URL=postgres://rexamp:password@pgbouncer:6432/redisexamp?sslmode=disable
REDIS=redis:6379 
SENTRY_DSN=your_sentry_dsn
$ make run-stack

For down this stack use:

$ make down-stack

For show running containers use:

$ make ps-stack
  • Prometheus collect metrics from go-redis-app at port 8081 on /metrics url
  • Also collect metrics from Redis, Pgbouncer and Fluent-Bit services by default settings for this services

Work with prometheus historgram on doc or blog post

Grafana Dashboards

Kubernetes Probes

Application export two url on monitoring port 8081 for k8s probes.

  • /live - for liveness probe, return 200 ok if live or 503 if dead
  • /ready - for readiness probe, return 200 ok if live and read to working or 503 if live and not ready to working