Back to Blog
Performance Architecture: Building for Speed
Performance Is Architecture
You can't optimize your way out of poor architecture. Performance needs to be a first-class architectural concern, not an afterthought.
Caching Architecture
// Multi-layer caching
class ProductService
{
public function get(int $id): Product
{
// L1: Request cache (same request)
$key = "product_{$id}";
if ($cached = RequestCache::get($key)) {
return $cached;
}
// L2: Application cache (Redis)
$product = Cache::remember($key, 3600, function () use ($id) {
return Product::find($id);
});
RequestCache::put($key, $product);
return $product;
}
}
Database Optimization
// Eager loading by default
class Order extends Model
{
protected $with = ['customer', 'items'];
}
// Query-specific eager loading
Order::with(['items.product', 'shipping'])->get();
// Select only needed columns
Order::select(['id', 'total', 'status'])->get();
Async Processing
// Move slow operations to queues
class CreateOrderController
{
public function __invoke(Request $request): Response
{
$order = Order::create($request->validated());
// Fast response, heavy work in background
ProcessOrderPayment::dispatch($order);
SendOrderNotifications::dispatch($order);
UpdateInventory::dispatch($order);
return response()->json(['order_id' => $order->id], 201);
}
}
Read Optimization
// Denormalized read models
Schema::create('order_summaries', function (Blueprint $table) {
$table->id();
$table->foreignId('order_id');
$table->string('customer_name');
$table->string('total_formatted');
$table->integer('items_count');
// Pre-computed, fast to read
});
// Keep in sync with events
class OrderSummaryProjector
{
public function onOrderPlaced(OrderPlaced $event): void
{
OrderSummary::create([...]);
}
}
Connection Pooling
// Use connection pooling for external services
class HttpClientPool
{
private array $clients = [];
public function get(string $service): Client
{
if (!isset($this->clients[$service])) {
$this->clients[$service] = new Client([
'base_uri' => config("services.{$service}.url"),
'pool_size' => 10,
]);
}
return $this->clients[$service];
}
}
Conclusion
Performance architecture means caching at multiple levels, optimizing database access patterns, moving slow operations to background processing, and designing read models for query efficiency.
Related Articles
Need Help With Your Project?
I respond to all inquiries within 24 hours. Let's discuss how I can help build your production-ready system.
Get In Touch