This repository demonstrate a backend-only TypeScript project illustrating how to implement Redis caching with PostgreSQL and Drizzle ORM.
- Node.js & TypeScript: Modern backend development with type safety.
- PostgreSQL: Robust relational database for persistent storage.
- Drizzle ORM: Lightweight and type-safe ORM for database interactions.
- Redis Caching: Improved performance by caching frequent database queries.
- Docker Support: Easy environment setup using Docker/Podman Compose.
- Runtime: Node.js (v22+)
- Language: TypeScript
- Framework: Express.js
- ORM: Drizzle ORM
- Database: PostgreSQL
- Cache: Redis
- Containerization: Podman/Docker Compose
The project implements the Cache-Aside (Lazy Loading) pattern for data retrieval and Cache Invalidation for data modifications.
sequenceDiagram
participant Client
participant Service
participant Redis
participant PostgreSQL
Client->>Service: Request Product Data
Service->>Redis: GET product:id
alt Cache Hit
Redis-->>Service: Return cached data
Service-->>Client: Return JSON (Source: Cache)
else Cache Miss
Redis-->>Service: Null
Service->>PostgreSQL: SELECT * FROM products WHERE id = ?
PostgreSQL-->>Service: Return product record
Service->>Redis: SET product:id (TTL: 60s)
Service-->>Client: Return JSON (Source: Database)
end
sequenceDiagram
participant Client
participant Service
participant PostgreSQL
participant Redis
Client->>Service: Update Product
Service->>PostgreSQL: UPDATE products SET ...
PostgreSQL-->>Service: Success
Service->>Redis: DEL product:id & products:all
Service-->>Client: Return updated product
- Clone the repository.
- Install dependencies:
npm install
The easiest way to run the project is using the provided compose file:
podman compose up -dor
docker-compose up -dThis will start:
- A PostgreSQL instance on port
5432 - A Redis instance on port
6379 - The Node.js application (if included in compose) on port
3000
-
Start the infrastructure (Database & Redis):
podman compose -f docker-compose.dev.yml up -d
-
Run database migrations:
npm run db:migrate
-
Start the development server:
npm run dev
src/config: Database and Redis client configurations.src/db: Schema definitions and migration scripts.src/services: Business logic and caching implementation.src/controllers: Request handling.src/routes: API route definitions.
npm run dev: Starts the server in development mode usingtsx.npm run build: Compiles the TypeScript code to JavaScript in thedistfolder.npm run start: Runs the compiled production build.npm run db:generate: Generates Drizzle migrations.npm run db:migrate: Executes migrations against the database.npm run db:push: Pushes schema changes directly to the database.