Microservices in PHP: Where to Start and Keep Your Sanity
\n\nHave you ever looked at a monolithic PHP application that has grown to 200,000 lines of code and felt your stomach tighten? One commit — and everything breaks. One error in the cart module — and the entire site goes down. Sound familiar? Welcome to the club of monolith survivors.
\n\nBut here's the good news: microservices in PHP are not rocket science. They are simply a set of small, independent applications that communicate with each other via HTTP or queues. And yes, PHP is perfectly suited for this. Don't believe me? Laravel, Symfony, and even "pure" PHP handle the role of microservices excellently.
\n\nIn this article, I'll show you microservice architecture in PHP with real code examples. You'll learn where to start, which tools to choose, and how to avoid common pitfalls. Let's go!
\n\nWhat are Microservices in PHP in Simple Terms
\n\nImagine a pizzeria. A monolith is when one chef does everything: kneads the dough, cuts the sausage, bakes, packages, takes orders, and washes dishes. If the chef gets sick — the pizzeria is closed. Microservices are when you have a separate chef for the dough, a separate one for the toppings, a separate one for the oven, and a separate delivery person. Each works autonomously, and if the "dough chef" gets sick, the others keep working.
\n\nIn the PHP world, it looks like this:
\n\n- \n
- User Service — registration, login, profiles \n
- Order Service — creation, payment, statuses \n
- Notification Service — email, SMS, push \n
- API Gateway — a single entry point for clients \n
Each service is a separate PHP application with its own database, its own repository, and its own logic. And all of this works together, forming a microservice architecture in PHP.
\n\nWhen Should You Switch to Microservices?
\n\nLet's be honest: microservices are not a silver bullet. If you have 3,000 users and 10 pages — forget it. You need a monolith. But if:
\n\n- \n
- The development team has grown to 5+ people \n
- You deploy every small change with prayers \n
- A single database has become a bottleneck \n
- Different parts of the application require different technologies (e.g., Elasticsearch for search and PostgreSQL for orders) \n
...then microservices in PHP are your choice.
\n\nWhere to Start: A Basic Microservice in PHP
\n\nLet's start simple. We'll create a microservice that returns a list of users. This will be a PHP REST API microservice.
\n\nStep 1: Project Structure
\n\nuser-service/\n├── public/\n│ └── index.php\n├── src/\n│ ├── Controllers/\n│ │ └── UserController.php\n│ ├── Models/\n│ │ └── User.php\n│ └── Services/\n│ └── UserService.php\n├── config/\n│ └── database.php\n├── composer.json\n└── .env\n\nStep 2: Entry Point (index.php)
\n\n<?php\n// public/index.php\nrequire_once __DIR__ . '/../vendor/autoload.php';\n\nuse Symfony\\Component\\Dotenv\\Dotenv;\nuse App\\Controllers\\UserController;\n\n$dotenv = new Dotenv();\n$dotenv->load(__DIR__ . '/../.env');\n\n$uri = $_SERVER['REQUEST_URI'];\n$method = $_SERVER['REQUEST_METHOD'];\n\n// Simple router\nif ($uri === '/api/users' && $method === 'GET') {\n $controller = new UserController();\n echo json_encode($controller->index());\n} elseif (preg_match('/\\/api\\/users\\/(\\d+)/', $uri, $matches) && $method === 'GET') {\n $controller = new UserController();\n echo json_encode($controller->show($matches[1]));\n} else {\n http_response_code(404);\n echo json_encode(['error' => 'Not found']);\n}\n\nStep 3: Controller
\n\n<?php\n// src/Controllers/UserController.php\nnamespace App\\Controllers;\n\nuse App\\Services\\UserService;\n\nclass UserController\n{\n