โšก The Performance Bible

The Architect's Handbook: From Code to Physics

"The language is just a tool. You don't blame the hammer if you can't build a skyscraper alone."
๐Ÿ“š 30 Topics
๐Ÿ“– 800+ Pages Equivalent
๐Ÿ’ป 200+ Code Examples
๐Ÿ‘ค EzEldeen A. Y. Mushtaha
Software Architect & Performance Engineer

๐Ÿ“… Originally compiled: 2026 โ€ข All content based on real-world production experience

๐Ÿ“– Volume I: Philosophical Foundations

Topic 01
Language vs Algorithm
The Golden Law of performance. Why mathematics beats physics.
  • Constants (Language Speed) vs Growth (Algorithm Complexity)
  • O(1) in Python beats O(nยฒ) in C++ by millions of times
  • Car & Road Analogy: Ferrari vs Toyota on different roads
  • When does language actually become the bottleneck?
๐Ÿ“– Read Topic 01 โ†’

๐Ÿ› ๏ธ Volume II: Laravel Performance Tuning Kit

Topic 02
Route Caching
Eliminate route parsing overhead on every request.
  • Without cache: 200 routes = 30ms regex parsing per request
  • With cache: routes precompiled as PHP array โ†’ 2ms
  • Command: php artisan route:cache
  • Part of php artisan optimize
๐Ÿ“– Read Topic 02 โ†’
Topic 03
Config Caching (env trap)
The #1 production killer after config:cache.
  • Never use env() outside config/*.php files
  • After config:cache, env() returns null
  • Always use config() in your code
  • Saves 15-40ms per request + eliminates I/O
๐Ÿ“– Read Topic 03 โ†’
Topic 04
Eloquent N+1 Killer
The #1 performance disaster in Laravel.
  • 101 queries when 2 would suffice
  • Solution: with() for eager loading
  • preventLazyLoading() catches N+1 in development
  • Hidden N+1 in views (the real trap)
๐Ÿ“– Read Topic 04 โ†’
Topic 05
Hydration Hell
Eloquent vs Query Builder memory comparison.
  • Eloquent: 500MB for 100k users
  • Query Builder: 50MB for 100k users
  • Read-only? Use DB::table() or raw PDO
  • 5-10x memory difference
๐Ÿ“– Read Topic 05 โ†’
Topic 06
Queue Serialization Trap
Why sending full models to queues kills Redis memory.
  • Full model serialization: 5KB per job
  • ID only: 8 bytes per job โ†’ 99.9% reduction
  • Always dispatch $user->id, not $user
  • Fetch fresh data inside the job
๐Ÿ“– Read Topic 06 โ†’
Topic 07
Service Container & Reflection
Why auto-wiring is expensive and how to fix it.
  • Reflection = code reading code at runtime (10-100x slower)
  • Singletons for stateless services
  • Explicit binding to skip reflection
  • Never call make() inside loops
๐Ÿ“– Read Topic 07 โ†’
Topic 08
Middleware Selectivity
Every request runs 10 middleware classes. It shouldn't.
  • Health check doesn't need session, auth, or CSRF
  • Create specialized groups: public, auth-api, web-session
  • Order matters: put fast-failing middleware first
  • Save 5-10ms per request
๐Ÿ“– Read Topic 08 โ†’
Topic 09
Database Indexing & EXPLAIN
The #1 database performance killer.
  • Missing index = full table scan = 5 seconds for 1M rows
  • With index = 0.001 seconds
  • Run EXPLAIN before any query
  • Composite index order matters (left-most prefix)
๐Ÿ“– Read Topic 09 โ†’
Topic 10
Read/Write Splitting
Master for writes, replicas for reads.
  • 90% of traffic is reads. Replicas handle them.
  • Laravel config: no code changes needed
  • Double your capacity overnight
  • Beware of replication lag (eventual consistency)
๐Ÿ“– Read Topic 10 โ†’
Topic 11
Database Partitioning
Vertical & Horizontal partitioning.
  • Vertical: split large columns (BLOB, TEXT) to separate tables
  • Horizontal: split by date or user_id (sharding)
  • MySQL native partitioning (by date, DROP PARTITION in seconds)
  • Application-level sharding for multi-server scale
๐Ÿ“– Read Topic 11 โ†’
Topic 12
Statelessness Doctrine
Your server must be killable at any moment.
  • Sessions โ†’ Redis (not files)
  • Files โ†’ S3 (not local disk)
  • Logs โ†’ CloudWatch / ELK
  • Config โ†’ Environment Variables
๐Ÿ“– Read Topic 12 โ†’
Topic 13
Event-Driven Architecture
Stop making users wait. Process in the background.
  • User waits: 500ms โ†’ 20ms (96% faster)
  • Dispatch events, return 202 Accepted
  • Queues + ShouldQueue for background processing
  • Scale workers independently
๐Ÿ“– Read Topic 13 โ†’
Topic 14
CDN & Edge Caching
Keep Laravel away from static files.
  • Nginx serves static files (1ms vs Laravel 50ms)
  • CDN caches globally (Tokyo: 150ms โ†’ 5ms)
  • Browser caching with Cache-Control headers
  • Asset versioning for cache busting
๐Ÿ“– Read Topic 14 โ†’
Topic 15
The 10 Disasters
The most common production failures.
  • DB::enableQueryLog() in production โ†’ OOM
  • env() after config:cache โ†’ returns null
  • User::all() for large datasets โ†’ memory explosion
  • Full model in queue โ†’ payload bloat
  • Forgotten optimize โ†’ 30ms waste per request
  • Lazy loading N+1 in views
  • Log::info() inside loops โ†’ I/O death
  • File session driver โ†’ concurrency killer
  • Missing indexes โ†’ full table scan
  • pm.max_requests = 0 โ†’ memory leaks
๐Ÿ“– Read Topic 15 โ†’
Topic 16
PHP-FPM Deep Dive
The real bottleneck nobody talks about.
  • pm.max_children calculation: (RAM - others) / 40MB
  • pm.max_requests = 500 (prevents memory leaks)
  • dynamic vs ondemand vs static
  • request_terminate_timeout = 60s
๐Ÿ“– Read Topic 16 โ†’
Topic 17
OpCache & PHP JIT
PHP's built-in performance superpowers.
  • OpCache: compile once, cache forever (3-5x faster)
  • JIT: compile hot paths to machine code (PHP 8+)
  • OpCache configuration for production
  • JIT for CPU-intensive apps only
๐Ÿ“– Read Topic 17 โ†’
Topic 18
Composer Autoloader
Stop PHP from searching for files.
  • --classmap-authoritative eliminates filesystem search
  • APCu autoloader caching: classmap stays in memory
  • Full production command: composer install --no-dev --optimize-autoloader --classmap-authoritative
  • 15-20ms โ†’ 2-4ms per request
๐Ÿ“– Read Topic 18 โ†’
Topic 19
Database Proxy (ProxySQL)
Connection pooling for MySQL.
  • Eliminate TCP handshake per request (1.5ms โ†’ 0.1ms)
  • Reuse connections across requests
  • Query caching in ProxySQL
  • Read/write splitting at proxy level
๐Ÿ“– Read Topic 19 โ†’
Topic 20
APCu vs Redis
Local shared memory vs network-based cache.
  • APCu: 0.05ยตs, Redis: 10ยตs (200x faster)
  • APCu for config, feature flags, local counters
  • Redis for sessions, queues, shared cache
  • Hybrid approach: APCu + Redis layered cache
๐Ÿ“– Read Topic 20 โ†’
Topic 21
Laravel Octane
The nuclear option. Keep Laravel alive in memory.
  • Swoole vs RoadRunner
  • Eliminate bootstrap overhead โ†’ 10x faster
  • Memory leak danger in persistent processes
  • Octane-safe code patterns
๐Ÿ“– Read Topic 21 โ†’
Topic 22
SplFixedArray
PHP arrays are hash maps. SplFixedArray is a true C array.
  • PHP array: 500MB for 1M integers
  • SplFixedArray: 100MB for 1M integers (80% less)
  • Fixed size, integer keys only
  • 20-40% faster than normal arrays
๐Ÿ“– Read Topic 22 โ†’
Topic 23
Lazy Resolution for Services
Don't pay for services you don't use.
  • Eager: all services instantiated on every request
  • Lazy: instantiate only when used
  • Bind with class name strings for lazy resolution
  • 70-80% reduction in service overhead
๐Ÿ“– Read Topic 23 โ†’
Topic 24
Observers & withoutEvents
The silent performance killers.
  • Without events: bulk update = 5ms
  • With observers: bulk update = 5,000ms (1000x slower)
  • Use withoutEvents() for bulk operations
  • Move observer logic to queued jobs
๐Ÿ“– Read Topic 24 โ†’
Topic 25
Logging I/O Trap
Every Log::info() opens a file.
  • 10,000 logs = 10,000 I/O operations (1-2 seconds)
  • Batch logs in arrays: 2 I/O operations (0.002 seconds)
  • Set LOG_LEVEL=error in production
  • Use Redis logging for high volume
๐Ÿ“– Read Topic 25 โ†’
Topic 26
Blade View Cache
Every Blade template is compiled to PHP.
  • Without cache: 5-20ms per view per request
  • With view:cache: 1-2ms per view
  • Command: php artisan view:cache
  • Part of php artisan optimize
๐Ÿ“– Read Topic 26 โ†’

๐ŸŒ Volume IV: Distributed Systems

Topic 27
CAP Theorem
Consistency vs Availability vs Partition Tolerance.
  • You can only have two of three
  • CP (banking): consistency over availability
  • AP (social media): availability over consistency
  • CA is impossible in distributed systems
๐Ÿ“– Read Topic 27 โ†’
Topic 28
Distributed Locking
Cache::lock is not enough for multi-server environments.
  • Redlock: 5 Redis instances for safety
  • ZooKeeper: strong consistency, fencing tokens
  • etcd: modern alternative (Kubernetes)
  • MySQL GET_LOCK: simple but slow
๐Ÿ“– Read Topic 28 โ†’
Topic 29
Observability
Logging, Metrics, Tracing โ€” the three pillars.
  • Logs: what happened (ELK, Loki)
  • Metrics: how often (Prometheus, Datadog)
  • Tracing: where time went (Jaeger, Tempo)
  • Correlation IDs connect everything
๐Ÿ“– Read Topic 29 โ†’
Topic 30
Zero-Trust Architecture
Trust nothing. Verify everything.
  • Death of the perimeter (no inside/outside)
  • Authenticate every request, every service
  • Least privilege, JIT access, micro-segmentation
  • Assume breach, monitor continuously
๐Ÿ“– Read Topic 30 โ†’