backend-patterns

Guides server-side code design covering layers, queries, transactions, caching, queues, auth, and logging.

Updated Aug 3, 2026
One-click install
npx skills add https://github.com/m-de-graaff/skills --skill backend-patterns-m-de-graaff
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: backend-patterns
Source: https://github.com/m-de-graaff/skills/tree/main/skills/backend-patterns
Command: npx skills add https://github.com/m-de-graaff/skills --skill backend-patterns-m-de-graaff

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Server-side code often accumulates business logic in HTTP handlers, hides N+1 query problems, leaks database errors to clients, and relies on in-process queues or caches that break under real deployment conditions. This Skill provides concrete patterns and a review checklist for writing and reviewing the code that lives between request parsing and response shaping. ## Core Features & Use Cases - Layering guidance: Keeps handlers thin by splitting logic into service and repository layers only when a second caller or testability justifies it. - Query and transaction discipline: Covers avoiding SELECT *, detecting N+1 patterns, indexing filtered columns, and keeping network calls out of transactions. - Operational patterns: Addresses cache invalidation, durable idempotent background jobs, domain-to-HTTP error mapping, per-resource authorization, structured logging, and shared-store rate limiting. - Use Case: While reviewing a list endpoint that loads related records in a loop, apply the N+1 batching pattern and verify indexes with EXPLAIN before shipping. ## Quick Start Ask the assistant to review your endpoint handler and data access code using the backend-patterns checklist for layering, queries, caching, and error handling.

Frequently Asked Questions about backend-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix N+1 queries in a list endpoint?▼

Replace per-row lookups with a batched fetch: collect the foreign keys from the result set, run one query for the related records, and join them in memory with a map. Also watch for ORM lazy relations accessed inside loops or serializers.

When should I split code into service and repository layers?▼

Split when a second caller appears or when you want to test business logic without a database. A repository with one caller and no second implementation is unnecessary indirection, and a service that only forwards calls adds no value.

Should authorization checks live in middleware or beside the query?▼

Middleware can enforce authentication and roles, but ownership checks must live beside the fetch because they depend on the specific resource. Return 404 rather than 403 for resources the user does not own to avoid confirming existence.

Can I use an in-process queue or cache in production?▼

Only for fire-and-forget work you can afford to lose in a single-process app, or for process-local data identical across replicas. Durable background work needs a real queue with retries and idempotent handlers, and rate limiting needs a shared store like Redis.

Why should database error messages not reach API clients?▼

Driver errors leak table names, column names, and sometimes values, exposing internal schema to attackers. Map domain errors to HTTP responses at one edge handler and return a generic 500 for anything unrecognised.