Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added src/problem4/.env
Empty file.
36 changes: 36 additions & 0 deletions src/problem4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Problem 4

A small platform has been deployed using Docker Compose.
Users are reporting that the API is unreliable and sometimes inaccessible.

Your task is to investigate and stabilize the system.

## Setup

Requirements:

* Docker
* Docker Compose

Run:

docker compose up --build

The application will be available at:
[http://localhost:8080](http://localhost:8080)

## Your Tasks

1. Identify the issues affecting the system
2. Fix them
3. Explain the root causes

## Submission

Submit a short report including:

* What problems you found
* How you diagnosed them
* The fixes you applied
* What monitoring/alerts you would add
* How you would prevent this in production
9 changes: 9 additions & 0 deletions src/problem4/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM node:20-alpine

WORKDIR /app
COPY package.json .
RUN npm install

COPY src ./src

CMD ["node", "src/index.js"]
9 changes: 9 additions & 0 deletions src/problem4/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "api",
"version": "1.0.0",
"dependencies": {
"express": "^4.18.2",
"pg": "^8.11.3",
"ioredis": "^5.3.2"
}
}
35 changes: 35 additions & 0 deletions src/problem4/api/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const express = require("express");
const { Pool } = require("pg");
const Redis = require("ioredis");

const app = express();

const pool = new Pool({
host: process.env.DB_HOST,
user: "postgres",
password: "postgres",
database: "postgres",
port: 5432,
});

const redis = new Redis({ host: process.env.REDIS_HOST, port: 6379 });

app.get("/api/users", async (req, res) => {
try {
const db = await pool.connect();
const result = await db.query("SELECT NOW()");
db.release();

await redis.set("last_call", Date.now());
res.json({ ok: true, time: result.rows[0] });
} catch (err) {
console.error(err);
res.status(500).json({ ok: false, error: err.message });
}
});

app.get("/status", (req, res) => {
res.json({ status: "ok" });
});

app.listen(3000, () => console.log("API running on 3000"));
28 changes: 28 additions & 0 deletions src/problem4/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version: "3.9"

services:
nginx:
image: nginx:1.25
ports:
- "8080:80"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
depends_on:
- api

api:
build: ./api
environment:
- DB_HOST=postgres
- REDIS_HOST=redis
depends_on:
- postgres
- redis

postgres:
image: postgres:15
environment:
POSTGRES_PASSWORD: postgres

redis:
image: redis:7
12 changes: 12 additions & 0 deletions src/problem4/nginx/conf.d/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server {
listen 80;

location = / {
return 200 "Welcome to the platform\n";
}

location /api/ {
proxy_pass http://api:3001;
}

}
Empty file added src/problem4/nginx/nginx.conf
Empty file.
1 change: 1 addition & 0 deletions src/problem4/postgres/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER SYSTEM SET max_connections = 20;