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
23 changes: 23 additions & 0 deletions optimist_weather/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Build
FROM golang:1.23.2-alpine3.19 AS buildenv

ADD go.mod go.sum /

RUN go mod download

WORKDIR /app

ADD . .

RUN go build -o main cmd/main.go

## Deploy
FROM alpine

WORKDIR /

COPY --from=buildenv /app/ /

EXPOSE 8081

CMD ["/main"]
143 changes: 143 additions & 0 deletions optimist_weather/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# **OptimistWeather**


## Abstract

---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Удалить эти симолы, они не входят в шаблон


This project focuses on optimizing weather query performance by leveraging **Redis** for caching and **PostgreSQL indexes** for
efficient database operations. It is designed for students to gain hands-on experience with query optimization techniques and database performance tuning using Go.


## **Learning Objectives**

---

- Understand the role of **Redis** in caching and reducing query load.
- Learn how to use **PostgreSQL indexes** to optimize query performance.
- Explore error handling and monitoring in Go applications.


## **Context**

---

### Background
Weather APIs often experience performance bottlenecks due to frequent requests for real-time data. This project demonstrates how caching
and indexing can significantly improve query efficiency and scalability.

### Real-World Applications
- Logistics and supply chain management
- Weather forecasting systems
- Disaster response platforms

### Technical Concepts
- **Caching:** Store frequently accessed data temporarily for quick retrieval.
- **Indexes:** Enhance database query performance by reducing the search space.

### Key Terms
- **Redis:** In-memory store used for caching and message brokering.
- **PostgreSQL Index:** A structure that accelerates data retrieval operations.


## **Resources**

---

- [Redis Documentation](https://redis.io/docs/)
- [PostgreSQL Indexing Guide](https://www.postgresql.org/docs/current/indexes.html)
- [Go Redis Client Library](https://github.com/go-redis/redis)
- [Cache strategies](https://medium.com/@mmoshikoo/cache-strategies-996e91c80303)



## **General Criteria**

---

- **Code Style:** Use `gofmt` for consistent formatting and adhere to Go idioms.
- **Compilation:** Ensure all code compiles cleanly using `go build`.
- **Execution:** Compatible with Linux or macOS systems; include Docker support.
- **Allowed Packages:**
- `github.com/go-redis/redis/v8`
- `github.com/lib/pq`
- **Disallowed Packages:** Frameworks that overly abstract caching or database operations.

### Build Command:
```bash
docker-compose up -d --build
```


## **Mandatory Features**

---

### **Outcomes**:

- Reduce database query load by caching frequently accessed weather data.
- Functionality: Store and retrieve weather data in Redis with configurable TTL (Time-To-Live) / LRU / LFU.
- Optimize database queries by creating and using PostgreSQL indexes.
- Create indexes on relevant columns to improve query execution speed.


### Notes:

---

- Input: Location (e.g., city name or coordinates).
- Output: Weather data in JSON format.
- Handle invalid inputs gracefully and log errors.
- Ensure cache expiration for data freshness.


## Constraints:

---

- Handle errors gracefully and return appropriate HTTP status codes.
- All actions and errors must be logged.


## Support Section

---

- You can do it

### Debugging Tips:
- Use redis-cli to monitor cache hits and misses.
- Run EXPLAIN/EXPLAIN ANALYZE in PostgreSQL to analyze query plans.
```sql
EXPLAIN ANALYZE SELECT * FROM weather_data WHERE city = 'Astana';
```


### Common Issues and Fixes:

- Cache misses: Verify key structure and TTL configuration.
- Slow queries: Check if indexes are missing or misconfigured.


### Where to Seek Help:
- Go forums and documentation
- Redis and PostgreSQL communities
- Stack Overflow for debugging Go applications



## API Endpoints

---

- TODO fill after

## This project has been created by:

Beknur Karshyga

Contacts:
- Email: [karshyga.beknur@gmail.com](mailto:karshyga.beknur@gmail.com)
- GitHub: [mrbelka12000](https://github.com/mrbelka12000)
- LinkedIn: [Beknur Karshyga](https://www.linkedin.com/in/beknur-karshyga/)

35 changes: 35 additions & 0 deletions optimist_weather/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: '2'
services:
redis__weather:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут специально 2 символна ниженего подчеркивания _?

image: redis
container_name: redis
ports:
- "6379:6379"
networks:
- optimist_weather
volumes:
- redis_data:/data

postgres_weather:
# TODO fill with weather data
image: postgres:latest
container_name: postgres_users
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "mrbelka12000"
POSTGRES_DB: "optimist_weather"
DATABASE_HOST: "localhost"
ports:
- '5432:5432'
volumes:
- postgresData:/var/lib/postgresql/data
networks:
- optimist_weather

volumes:
redis_data:
postgresData:
Comment on lines +30 to +31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Свести к consistency, чтобы названия ресурсов были либо в camelCase или snake_case


networks:
optimist_weather:
driver: bridge