A comprehensive, production-ready implementation of 10 database architecture patterns, demonstrating the evolution from simple single-database setups to globally distributed enterprise systems.
This project provides working implementations of progressively complex database architectures, each building upon the previous layer. Perfect for learning, reference, or as a foundation for production systems.
All 10 layers are fully implemented with:
- β Production-ready code with best practices
- β 200+ comprehensive tests (all passing)
- β Complete Docker support for each layer
- β Detailed documentation with architecture diagrams
- β Real-world patterns used by major companies
| Layer | Name | Key Features | Complexity | Use Case |
|---|---|---|---|---|
| L0 | Single DB | Direct PostgreSQL access | β | MVP, Prototypes |
| L1 | Connection Pooling | psycopg pools | β | Small apps |
| L2 | Read Cache | Redis cache-aside | ββ | Growing apps |
| L3 | Read Replicas | PostgreSQL replication | βββ | Medium scale |
| L4 | DB Sharding | Hash-based partitioning | ββββ | High scale |
| L5 | Multi-Tier Cache | L1 + L2 caching | βββ | High performance |
| L6 | Write Buffering | Async writes with queues | ββββ | High write load |
| L7 | CQRS | Separate read/write DBs | ββββ | Complex domains |
| L8 | Polyglot | PostgreSQL + MongoDB + Redis | βββββ | Multiple data types |
| L9 | Global Distributed | Multi-region replication | βββββ | Worldwide scale |
| L10 | Enterprise-Grade | Full stack with all features | βββββ | Production at scale |
- Docker & Docker Compose (recommended)
- Python 3.11+ (for local development)
- PostgreSQL 15 (if running without Docker)
- Redis 7 (if running without Docker)
# Clone the repository
git clone https://github.com/rohansen856/database-layering.git
cd database-layering
# Navigate to any layer
cd l<N>-<name>/
# Start with Docker (recommended)
docker-compose up -d
# Check health
curl http://localhost:800<N>/health
# Run tests
docker-compose exec api pytest -v
# View logs
docker-compose logs -f api
# Stop services
docker-compose downcd l5-multi-tier-cache/
docker-compose up -d
# Write data
curl -X POST http://localhost:8005/write \
-H "Content-Type: application/json" \
-d '{"key":"user:123","value":"John Doe"}'
# Read data (will use cache)
curl http://localhost:8005/read/user:123
# View cache statistics
curl http://localhost:8005/cache-stats
# Run tests
docker-compose exec api pytest -v
# Cleanup
docker-compose downWhat: Direct database connection for all operations When: MVPs, prototypes, learning Performance: Baseline (10-50ms per operation)
What: Reuse database connections for better concurrency When: Any production application Performance: +20% throughput improvement
What: Redis cache to reduce database load When: Read-heavy workloads (70%+ reads) Performance: 60-90% faster reads, 80% DB load reduction
What: Distribute reads across replica databases When: High read volume (100K+ requests/day) Performance: Horizontal read scaling
What: Partition data across multiple databases When: Data doesn't fit on single server Performance: Linear scaling with data size
What: L1 (in-process) + L2 (Redis) caching When: Ultra-low latency requirements Performance: Sub-millisecond reads
What: Queue writes for async processing When: High write throughput needed Performance: 10x write throughput
What: Separate optimized read and write databases When: Complex business logic, different read/write patterns Performance: Independent optimization of reads and writes
What: PostgreSQL + MongoDB + Redis for different data types When: Multiple data access patterns Performance: Optimized per workload
What: Multi-region databases with geo-routing When: Global user base Performance: 70-90% latency reduction worldwide
What: Complete stack with auth, rate limiting, metrics, circuit breakers When: Production at scale Performance: 99.99% uptime, predictable latency
- Databases: PostgreSQL 15, MongoDB 7, Redis 7
- Framework: FastAPI (Python)
- Testing: pytest with 200+ tests
- Monitoring: Prometheus metrics (Layer 10)
- Infrastructure: Docker & Docker Compose
- LAYERS.md - Detailed architecture specifications
- TESTING.md - Comprehensive testing guide (pytest + manual)
- LOAD-TESTING.md - Load testing guide using oha
- LOAD-TEST-QUICK-START.md - Quick reference for load testing
- DOCKER-TESTING.md - Docker testing guide with examples
- FIXES-DECEMBER-2025.md - Recent fixes and improvements
- VERIFICATION.md - Verification commands and expected outputs
- Each layer's README - Layer-specific documentation
- CONTRIBUTING.md - How to contribute
- LICENSE - MIT License
Each layer includes comprehensive tests:
# Run all tests for a specific layer
cd l<N>-<name>/
docker-compose exec api pytest -v
# Run tests without Docker
pytest -v
# Run specific test
pytest tests/test_*.py::test_specific_function -vTest Coverage: 200+ tests across all layers
Performance testing using oha:
# Install oha
cargo install oha
# Load test a specific layer
./load-test.sh 5
# Custom parameters (5000 requests, 100 concurrency, 30 seconds)
./load-test.sh -n 5000 -c 100 -d 30 5
# Test only writes
./load-test.sh -t write 2
# Test only reads
./load-test.sh -t read 5
# Load test all layers
./load-test.shSee LOAD-TESTING.md for detailed load testing documentation.
Beginners: Start with L0-L2 to understand fundamentals Intermediate: Progress through L3-L5 for scaling concepts Advanced: Study L6-L9 for complex distributed patterns Enterprise: Explore L10 for production-grade systems
| Metric | L0 | L2 | L5 | L9 | L10 |
|---|---|---|---|---|---|
| Read Latency | 10-50ms | 1-5ms | <1ms | <10ms | <1ms |
| Write Latency | 10-50ms | 10-40ms | 10-40ms | 10-50ms | 1-5ms |
| Throughput | 1K/s | 5K/s | 10K/s | 50K/s | 100K/s |
| Availability | 99% | 99.9% | 99.9% | 99.99% | 99.99% |
Contributions are welcome! Please see CONTRIBUTING.md for details.
This project is licensed under the MIT License - see the LICENSE file for details.
Caching: L2, L3, L4, L5, L7, L8, L9, L10 Replication: L3, L9 Sharding: L4, L10 Async Operations: L6, L7 Multiple DBs: L7, L8 Multi-Region: L9 Rate Limiting: L10 Circuit Breakers: L10 Metrics: L10 Authentication: L10
- Issues: Use GitHub Issues for bug reports
- Discussions: Use GitHub Discussions for questions
- Documentation: Check each layer's README.md
- Learning: Understand database architecture evolution
- Reference: Production-ready code examples
- Foundation: Start a new project with the right architecture
- Migration: Understand how to evolve your current system
- Architecture Specifications
- Contributing Guidelines
- License
- Layer 0 - Start Here
- Layer 10 - Enterprise
Built with β€οΈ for the developer community
Complete database architecture evolution from prototype to production