- Knowledge
- technology
- OOP
- Tips
- Programming
- Tips
- Tutorial
- SEO
- Ranking
- Knowledge
- Special Day
- Seo
- Bug
- Data science
- Seo
- artificial intelligence
- Machine Learning
- Robotics
- happyNewYear2021
- newYearEve
- 2021
- Automation
- Smart Home
- Career
- Best Practices
- Git
- Logging
- Web Fundamentals
- DNS
- HTTPS
- Performance
- AI Tools
- ChatGPT
- Claude
- Gemini
- Laravel
- Eloquent
- MySQL
- HTTPS
- TLS
- Web Security
- Certificates
- Developer Life
- Debugging
- Docker
- DevOps
- Transactions
- Queues
- LLMs
- AI
- AI Coding
- Developer Tools
- React Native
- Expo
- Kate PMS
- Mobile Apps
- Laravel
- Authentication
- Sanctum
- Cookies
- API Design
- Payments
- Idempotency
- DeepSeek
- Open Source AI
- LLMs
- AI News
- Git
- Version Control
- AI Coding
- Prompting
- PHP
- Checklist
- MCP
- AI Agents
- OpenAI
- Architecture
- Microservices
- Modular Monolith
- Estimation
- Developer Life
- Project Planning
- Humour
- OAuth
- OpenID Connect
- Authentication
- Embeddings
- Vector Search
- RAG
- pgvector
- OpenAI
- GPT-4.1
- Codex CLI
- Events
- Testing
- Clean Code
- Maintainability
- Code Review
- Webhooks
- API
- Security
- Claude Code
- Workflow
- AI
- LLM
- Prompt Injection
- Mobile
- React
- Networking
- TCP
- UDP
- HTTP/3
- CLAUDE.md
- AWS
- Cloud Security
- Backups
- PHPUnit
- Software Engineering
- Leadership
- Communication
- RAG
- Embeddings
- AI Engineering
- IT Infrastructure
- Networking
- Access Control
- CI/CD
- GitHub Actions
- Gemini CLI
- Claude Code
- JavaScript
- Async/Await
- Node.js
- Promises
- Security
- Cryptography
- Passwords
- MySQL
- Database
- Vibe Coding
- Software Quality
- DNS
- Code Reading
- Onboarding
- Productivity
- Background Jobs
- Developer Humour
- Estimates
- Dev Life
- JWT
- o3-mini
- DeepSeek R1
- Rate Limiting
- Kate PMS
- E-Signing
- Audit Trail
- REST
- GraphQL
- API Design
- Laravel 12
- Upgrade Guide
- Open Source
- Self-Hosting
- Task Scheduling
- Cron
- Secrets
- CORS
- PHP
- PHP-FPM
- OPcache
- GitHub Copilot
- Software Architecture
- Engineering
- TypeScript
- JavaScript
- Type Safety
- AI Security
- React Native
- Product Design
- AI Agents
- Kiro
- Queues
- Redis
- RabbitMQ
- AWS SQS
- Nginx
- Apache
- GPT-5
- gpt-oss
- Clean Code
- Architecture
- Naming
- Documentation
- Career
- ADR
- Teamwork
- Supply Chain
- Kate HRM
- HR Software
- Permissions
- System Design
- Pagination
- SSH
- Linux
- Big O
- Databases
- Laravel Boost
- MCP
- Developer Skills
- Validation
- Databases
- Indexes
- Code Quality
- Deployment
- Developer Humour
- Feature Flags
- Code Review
- Pull Requests
- Docker
- Cursor
- Authorization
- RBAC
- Gemini
- Long Context
- PHP 8.4
- Caching
- Dependency Injection
- Web Performance
- Browser
- CSS
- Database
- Migrations
- ChatGPT
- AI for Developers
- Monitoring
- On-Call
- REST
- Backend
- SQL
- NoSQL
- Database Design
- Coding Agents
- Claude 4
- API Resources
- REST API
- Load Balancing
- Scaling
- AWS
- AI Tools
- Claude
- Sora 2
- CTE
- 2FA
- TOTP
- Programming Languages
- Prompts
- Developer Workflow
- API Gateway
- APIs
- Passport
- API Auth
- Learning
- Burnout
- Developer Growth
- Web Development
- SEO
- Kate Mall
- ChatGPT Atlas
- Agent Skills
- Middleware
- Laravel 12
- Collections
- Context Window
- Monitoring
- Commit Messages
- Self Review
- Growth
- Regex
- Programming Basics
- Text Processing
- Database Design
- Normalization
- Linux
- Server Security
- Linux Foundation
- Open Standards
- Legacy Code
- Documentation
- AI Workflow
- File Uploads
- Test Data
- Hashing
- Performance
- Caching
- Enums
- Scope Creep
- Estimation
- Codex
- Gemini CLI
- Timezones
- Carbon
- Bugs
- PHP 8.5
- Gemini 3
- GPT-5.1
- Data Integrity
- Event Loop
- Async
- Opus 4.5
- AI Models
- React
- Forms
- Frontend
- Backups
- AI Images
- DALL-E
- Midjourney
- Race Conditions
- Concurrency
- Legacy Code
- Refactoring
- Senior Engineer
- Scope
- LLM
- CDN
- Web
- Sub-Agents
- Soft Deletes
- Audit Log
- Concurrency
- AI Learning
- NestJS
- AI Evals
- Policies
- SPF DKIM DMARC
- Unicode
- UTF-8
- Knowledge Graph
- Value Objects
- Technical Debt
- Feature Flags
- Laravel Pennant
- Deployment
- Copilot
- Composer
- Dependencies
- Artisan
- Automation
- AWS S3
- Object Storage
- Cloud
- Small Language Models
- Ollama
- Production
- Sessions
- HTTP
- Mentoring
- SQL
- Virtual Machines
- Web Development
- HTTP/2
- QUIC
- Web Performance
- AI Integration
- LLM API
- SOLID
- OOP
- Hosting
- Serverless
- Merge Conflicts
- Temperature
- AI Development
- Reverse Proxy
- Nginx
- Infrastructure
- Verification
- Passkeys
- WebAuthn
- Teams
- Communication
- Stakeholders
- Monorepo
- CI/CD
- Versioning
- JSON Schema
- Livewire
- Inertia
- Meetings
- Distributed Systems
- Privacy
- Full-Stack
- T-Shaped Skills
- Money
- Notifications
- Web Security
- HTTP Headers
- CSP
- Function Calling
- Load Testing
- k6
- Data Extraction
- Debugging
- WebSockets
- SSE
- Real-Time
- Laravel Reverb
- Infrastructure as Code
- Terraform
- Side Projects
- Laravel Pint
- OpenAPI
- Swagger
- UX
- Multimodal
- Jest
- Pair Programming
- APIs
- Rate Limiting
- Resilience
- Dev Humour
- Design Tokens
- JWT
- API Keys
- Sessions
- PHPStan
- Rector
- Incidents
- Reporting
- Dashboards
- Zero Trust
- IAM
- Search
- Laravel Scout
- Junior Developers
- Mentoring
- Images
- WebP
- AVIF
- Bug Reports
- Let's Encrypt
- Design Docs
- Software Design
- Observers
- Replication
- Accountability
- Data Structures
- Reliability
- LLM Memory
- Error Handling
- Payments
- Payment Gateway
- Webhooks
- PCI DSS
- Observability
- OpenTelemetry
- Personal Brand
- Writing
- Conventions
- Dates
- Scheduling
- Disaster Recovery
- Compression
- Brotli
- Deadlines
- Developer Habits
- State Machines
- Tech Roles
- UUID
- ULID
- Horizon
- Planning
- Engineering Culture
- Ownership
- Soft Skills
- Socialite
- Cost Control
- Collations
- Unicode
- Octane
- PostgreSQL
Content Delivery Networks: How a CDN Makes Your Site Faster (and How It Can Bite)
About Post
Your server is in one city. Your users are everywhere else. Every image, script and stylesheet has to cross that distance, and no amount of code optimisation can make light travel faster through a cable under the ocean.
A Content Delivery Network doesn't try. It moves copies of your files closer to the people asking for them. It's one of the cheapest, biggest speed-ups a website can get, and also one of the easiest ways to accidentally show one user another user's page. Let's look at both sides.
The idea: a warehouse in every neighbourhood
Imagine an online shop with one warehouse. Every order, from anywhere, ships from there. Now imagine small local depots stocked with the most popular items. Most orders arrive the next morning from down the road, and only unusual items come from the main warehouse.
A CDN is that network of depots. They're called edge locations (or points of presence), spread around the world. Your own server is the origin, the main warehouse.
One request, step by step
- A user in Doha opens your site. DNS for
cdn.example.com(or your whole domain) points to the CDN, which routes them to a nearby edge location. - The browser asks that edge for
/build/app-4f9a1c.js. - Cache hit: the edge already has it and returns it immediately. Your origin never hears about the request.
- Cache miss: the edge fetches the file from your origin, returns it to the user, and keeps a copy for the next person nearby, for as long as your cache rules allow.
The speed comes from three places. Shorter distance for the data itself. The secure connection being set up with a nearby edge instead of a faraway server, which matters because a TLS handshake takes several round trips. And your origin doing far less work, because most requests never reach it.
Your headers are the instructions
A CDN mostly does what your Cache-Control headers tell it to. Get these right and most of the work is done:
# Fingerprinted assets (app-4f9a1c.js): cache "forever"
Cache-Control: public, max-age=31536000, immutable
# HTML pages: always check with the server first
Cache-Control: no-cache
# A public API response that's the same for everyone
Cache-Control: public, s-maxage=60, stale-while-revalidate=30
# Anything personal
Cache-Control: private, no-store
max-ageapplies to browsers and CDNs;s-maxageapplies only to shared caches like the CDN, so you can cache longer at the edge than in the browser.no-cachedoesn't mean "don't cache". It means "cache, but check it's still fresh before using it".no-storeis the real "never keep this".stale-while-revalidatelets the cache serve a slightly old copy instantly while fetching a fresh one in the background.
In Laravel, the cache.headers middleware sets these for a route: Route::get('/api/listings', ListingIndexController::class)->middleware('cache.headers:public;max_age=300;etag').
What to put on a CDN
| Content | Cache at the edge? | Why |
|---|---|---|
| JS, CSS, fonts with hashed names | Yes, for a very long time | The name changes when the content changes |
| Images and media | Yes | Usually the heaviest part of a page |
| Public HTML (blog, marketing pages) | Yes, briefly, or with revalidation | Big speed win, but content changes |
| Public API data (listings, prices shown to everyone) | Sometimes, for seconds or minutes | Absorbs spikes; staleness must be acceptable |
| Dashboards, account pages, anything per user | No | Risk of serving one user's data to another |
Invalidation: the hard part, made easy
"There are only two hard things in computer science: cache invalidation and naming things." With a CDN, you can mostly avoid the first one.
The trick is fingerprinted file names. Laravel's Vite setup already does this: build output goes to public/build with a content hash in every name, and @vite writes the right names into your HTML. A new deploy means new names, so there's nothing old to invalidate. The old files just stop being requested.
For things that keep the same URL (an uploaded image someone replaced, an HTML page), you either use short cache times or purge the path through the CDN's API or dashboard. Purges work, but they take a moment to spread across all edges and some providers limit or charge for them, so they're a tool, not a strategy.
Images: the quiet giant
Images are often most of a page's weight. Many CDNs can transform them at the edge: resize to the width actually displayed, convert to WebP or AVIF for browsers that support them, and compress. You upload one large original; each device gets a version that fits. Even without that, serving images from a CDN with long cache times is one of the easiest wins there is.
Security comes along for the ride
- DDoS absorption. A CDN's network is far bigger than your server, so a flood of traffic hits the edge, not your origin.
- A Web Application Firewall at the edge can block common attacks and abusive bots before they reach your app.
- TLS everywhere, with modern protocols handled by the CDN.
- A hidden origin. Lock your origin down so it only accepts traffic from the CDN. For S3 behind CloudFront, origin access control does exactly that, so nobody can bypass the CDN and hit the bucket directly.
The mistakes that hurt
Caching personal pages. The classic incident: a page with a user's name or account data gets cached publicly, and the next visitor sees it. Logged-in pages should send private or no-store, and your CDN rules should never cache responses that set or depend on session cookies.
Ignoring the cache key. The CDN decides "is this the same request?" using the URL and whatever headers or cookies you include in the key. Leave out something that changes the response (like language) and users get the wrong version. Include too much (like every cookie, or User-Agent) and nothing is ever a hit.
Forgetting the origin still matters. A CDN hides a slow origin only for cacheable content. Your API and logged-in pages are as fast as your server, wherever the user is.
The rule: cache static, fingerprinted files for as long as possible, cache public pages briefly, and never cache anything personal. Your Cache-Control headers are the contract; the CDN just follows it.
Quick start checklist
- Serve built assets with hashed names and a one-year,
immutablecache. - Put images behind the CDN, ideally with resizing and modern formats.
- Mark personal responses
privateorno-store. - Lock the origin so only the CDN can reach it.
- Watch the cache hit ratio; if it's low, look at your cache key.
Have you ever been bitten by a CDN serving the wrong thing? I'd love to hear whether it was stale content or something worse.

Be first to comment it...