Backend Developer Cheat Sheet
APIs, databases, and server-side development quick reference.
Quick Reference
# Test API endpoint
curl -s http://localhost:8000/api/health | jq .
# Database quick check
psql -c "SELECT count(*) FROM users;"
# Run tests
pytest tests/ -v --tb=shortREST API Design
HTTP Methods
| Method | Use Case | Idempotent |
|---|---|---|
| GET | Read resource | Yes |
| POST | Create resource | No |
| PUT | Replace resource | Yes |
| PATCH | Update resource | No |
| DELETE | Remove resource | Yes |
Status Codes
| Code | Meaning | Use When |
|---|---|---|
| 200 | OK | Success |
| 201 | Created | Resource created |
| 204 | No Content | Success, no body |
| 400 | Bad Request | Invalid input |
| 401 | Unauthorized | Auth required |
| 403 | Forbidden | No permission |
| 404 | Not Found | Resource missing |
| 422 | Unprocessable | Validation failed |
| 500 | Server Error | Bug/crash |
cURL Commands
# GET request
curl -s http://api/users | jq .
# POST with JSON
curl -X POST http://api/users \
-H "Content-Type: application/json" \
-d '{"name":"John","email":"[email protected]"}'
# With auth token
curl -H "Authorization: Bearer TOKEN" http://api/users
# PUT request
curl -X PUT http://api/users/1 \
-H "Content-Type: application/json" \
-d '{"name":"Updated"}'
# DELETE
curl -X DELETE http://api/users/1
# Verbose output
curl -v http://api/health
# Time request
curl -w "@curl-format.txt" -o /dev/null -s http://api/usersSQL Quick Reference
Common Queries
-- Select with joins
SELECT u.name, o.total
FROM users u
JOIN orders o ON u.id = o.user_id
WHERE o.created_at > NOW() - INTERVAL '7 days';
-- Aggregation
SELECT status, COUNT(*), AVG(total)
FROM orders
GROUP BY status
HAVING COUNT(*) > 10;
-- Subquery
SELECT * FROM users
WHERE id IN (SELECT user_id FROM orders WHERE total > 100);
-- Window function
SELECT name, total,
RANK() OVER (ORDER BY total DESC) as rank
FROM orders;Performance
-- Explain query plan
EXPLAIN ANALYZE SELECT * FROM users WHERE email = '[email protected]';
-- Index usage
SELECT indexrelname, idx_scan, idx_tup_read
FROM pg_stat_user_indexes;
-- Table stats
SELECT relname, n_tup_ins, n_tup_upd, n_tup_del
FROM pg_stat_user_tables;
-- Current queries
SELECT pid, query, state, wait_event_type
FROM pg_stat_activity
WHERE state != 'idle';Redis Commands
# Connection
redis-cli
redis-cli -h host -p 6379 -a password
# Strings
SET key "value"
GET key
SETEX key 3600 "value" # With TTL
# Lists
LPUSH mylist "item"
LRANGE mylist 0 -1
# Hashes
HSET user:1 name "John"
HGET user:1 name
HGETALL user:1
# Cache pattern
GET cache:users:1 || (fetch from DB, SET cache:users:1)
# Keys
KEYS "pattern:*"
TTL key
DEL key
FLUSHDB # Clear current DBAuthentication
JWT Structure
Header.Payload.Signature
Header: {"alg":"HS256","typ":"JWT"}
Payload: {"sub":"1234567890","name":"John","iat":1516239022}Token Verification
import jwt
# Decode
payload = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
# Create
token = jwt.encode({"sub": user_id, "exp": expiry}, SECRET_KEY)Testing
# Run tests
pytest tests/ -v
pytest tests/test_users.py -k "test_create"
pytest tests/ --cov=app --cov-report=html
# Mock external services
# Use pytest-mock, responses, or httprettyCommon Patterns
# Pagination
GET /users?page=1&per_page=20
# Filtering
GET /users?status=active&role=admin
# Sorting
GET /users?sort=-created_at
# Rate limiting headers
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-RateLimit-Reset: 1609459200intermediateDeveloper RolesUpdated 2025-01-15
- backend
- api
- rest
- database
- sql
- server