Search for a command to run...

In Part 1, we followed a request from your browser to my server's door. This post opens the door. Inside the api container sits an ASP.NET Core 8 backend built with Clean Architecture and CQRS via MediatR — the same structure I use in my day job, applied to a blog on purpose: a small system is where you prove you understand a pattern before you trust it on a big one.
The backend solution is split into projects with one strict rule — dependencies point inward, and the domain knows nothing about the database, the cache, or HTTP:
ElHakim.Domain — entities and value objects. No framework dependencies; it does not know EF Core exists.
ElHakim.Application — the use cases: CQRS handlers, DTOs, and the interfaces (IAppDbContext, ICacheService) that the outer layers must implement.
ElHakim.Infrastructure — the implementations: EF Core against PostgreSQL, Redis caching, identity.
ElHakim.Contracts — shared request/response contracts between the API and its consumers.
ElHakim.Api — the thin shell: controllers, middleware, ETag filters. No business logic lives here.
A small example of why the Domain layer earns its keep: a post's slug is not a string in this codebase, it is a Slug value object. It is created in one place, validates itself — lowercase letters, numbers, hyphens, no surprises — and every query, command, and cache key uses it. The rule for what a valid slug looks like exists exactly once, so no controller can invent a different one.
Every operation in the API is either a Command — it changes state — or a Query — it returns data. Never a method that does both. MediatR dispatches each request to exactly one handler. This is not theory on this codebase: I scanned the backend with CqrsLens, a static-analysis tool I build, and the counts are concrete — 63 requests (39 commands, 24 queries), 63 handlers, 9 validators, and 60 HTTP endpoints mapped to them.
When this page loaded, the frontend asked the API for the post by slug. The controller is deliberately boring: it builds a GetPostBySlugQuery and hands it to MediatR. The handler does the real work, in this order:
Builds the cache key post-{slug}-{culture} and checks Redis.
On a miss, runs a pre-compiled EF Core query — the query plan is built once, not re-translated on every request.
Picks the translation: the requested culture first, English as fallback, then any available translation.
Caches the result for ten minutes and returns it.
Here is the core of the real handler:
One detail in that handler matters more than it looks: the cached object is shared by all readers, so it must never contain anything personal. Your like on this post is overlaid afterwards, per request, from a separate query — it is never written into the shared cache entry. Cache the public shape; personalize outside the cache.
Measured on the server, a post read served from Redis takes about 1–2 ms; a read that falls through to PostgreSQL takes about 5–10 ms. Two pieces of this path have their own deep dives: the CQRS audit of this backend, and why compiled queries are the cheapest performance win in EF Core.
Writes travel the same MediatR pipeline, and this is where the pipeline earns its name: a global ValidationBehavior runs each request's FluentValidation validator before its handler. A command with a malformed slug or no translations fails there — the handler, and the database, never see it.
After a successful write, the handlers clean up after themselves. Updating a post removes its cached entries (post-{slug}-en and post-{slug}-ar) and the cached post lists, so the next read repopulates Redis with fresh data instead of serving stale content until the TTL expires. Expiry is the safety net; invalidation is the mechanism.
The API runs from the same Docker Compose stack as Part 1 — one container next to PostgreSQL and Redis. On startup it applies any pending EF Core migrations itself, so a deploy is: build the new image, restart the one container, and the schema is current. A GitHub Actions workflow builds and tests the solution on every push; the deploy itself is a deliberate manual step. On a one-person platform, I would rather press the button myself than debug an automatic deploy at midnight.
Honestly: a blog does not need five projects and a mediator. A single project with controllers talking to EF Core would serve these pages. What the structure buys me is discipline I can verify — handlers that can be unit-tested without a database, a domain that cannot be reached around, validation that no new endpoint can forget, and a read path I can reason about from the controller to the SQL. Those are the same properties I need on the systems I build at work; the blog is where I keep them sharp.
Inside the API from Part 1: an ASP.NET Core 8 backend in Clean Architecture and CQRS — 63 requests, 63 handlers, one validation pipeline — and the real handler code that serves every post on Hakim.
I've shipped CQRS with MediatR in two real systems — and deliberately left it out of a third. Then I pointed my own CQRS mapping tool at this blog, and it showed me where my pipeline was bluffing.
When you open abdullahhakim.me, your request crosses Cloudflare, a Next.js frontend on Vercel, a tunnel into a small server I run myself, and an ASP.NET Core API backed by PostgreSQL and Redis. This post traces that exact path — the real one, as it runs today.