Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Microservice within a Monolith #devdays2019
Search
Joop Lammerts
May 15, 2019
Programming
92
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Microservice within a Monolith #devdays2019
Joop Lammerts
May 15, 2019
More Decks by Joop Lammerts
See All by Joop Lammerts
_Rootnet__You_re_Agile_is_broken__and_here_is_how_to_fix_it.pdf
jlammerts
0
60
How to improve your team synergy w/The Attitude Model #DPC19
jlammerts
0
210
Microservice within a Monolith #phpday
jlammerts
0
190
Microservice within a Monolith v1
jlammerts
0
150
The Attitude Model
jlammerts
0
43
Improve your team synergy w/The Attitude model
jlammerts
0
390
Other Decks in Programming
See All in Programming
What's New in Android 2026
veronikapj
0
260
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.9k
AWS CDK を「作」ってみた 〜フルスクラッチで見えた CDK の裏側〜 / aws-cdk-from-scratch
gotok365
3
2.8k
the container ship “Apple Silicon”@WWDC26 Recap -Japan-\(region).swift
shingangan
0
120
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
890
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
150
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
140
Android CLI
fornewid
0
220
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
570
php-fpmのプロセスが枯渇した日-調査・対処・そして本当にやるべきだったこと-
shibuchaaaan
0
290
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
210
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
380
Featured
See All Featured
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
470
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Why Our Code Smells
bkeepers
PRO
340
58k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
170
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2.3k
The Spectacular Lies of Maps
axbom
PRO
1
910
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Prompt Engineering for Job Search
mfonobong
0
400
How to Talk to Developers About Accessibility
jct
2
510
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
Context Engineering - Making Every Token Count
addyosmani
9
1.1k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
860
Transcript
Microservices within a Monolith Join at slido.com #devdays2019 @jlammerts
Joop Lammerts Developer @procurios for +3 years @jlammerts
Procurios Cluster
Procurios Cluster for context
Our Monolith
Our monolith ~ backend: 3.000.000 lines of code distributed over
18.000 PHP files ~ frontend: 350.000 lines in 1800 JavaScript files 800.000 lines of CSS code
Usage
Usage • 2000 clients • 800.000 users • 500.000 visitors
an hour
None
Monolith
None
Microservices
Service #1 Service #2 Service #3
Microservices, or microservice architecture, is an approach to application development
in which a large application is built as a suite of modular components or services. Assumption
Microservices, or microservice architecture, is an approach to application development
in which a large application is built as a suite of modular components or services. Assumption
Modules
Modular Monolith
Modular Monolith • Bounded context with no dependencies on each
other • Information can be duplicated for each bounded context
None
What to do with your legacy Monolith?
Project |--- core |--- modules |--- cms |--- meeting |---
relation |--- user
Project |--- core |--- modules |--- cms |--- meeting ->
attendee -> meeting -> registration -> ticket |--- relation |--- user
final class RegistrationController { public function register() { $userId =
$_SESSION['userId']; $pdo = new \PDO('localhost'); $statement = $pdo->prepare(" SELECT * FROM `user` WHERE `id` = ? "); $statement->execute([$userId]); $userData = $statement->fetch()[0]; if (!$userData) { HttpResponse::redirect('/login'); } $form = $this->getRegistrationForm(); $data = $form->getData(); if (!$data['meetingId'] || !$data['ticketId'] || !$data['remark']) { return $form; } $statement = $pdo->prepare(" UPDATE `tickets` SET `sold` = 1 WHERE WHERE `id` = ? "); $statement->execute([ $data['ticketId'] ]); if ($statement->rowCount() !== 1) { return 'There are no tickets available'; } $statement = $pdo->prepare(" INSERT INTO `attendee` SET `user_id` = ?, `first_name` = ?, `last_name` = ?, `meeting_id` = ?, `ticket_id` = ?, `remark` = ?, "); $statement->execute([ $userData['id'], $userData['firstName'], $userData['name'], $data['meetingId'], $data['ticketId'], $data['remark'], ]); /* * send confirmation stuff */ \HttpResponse::redirect('/'); } }
namespace Meeting\Registration; final class RegistrationController { public function register() {
$userId = $_SESSION['userId']; $pdo = new \PDO('localhost'); $statement = $pdo->prepare(" SELECT * FROM `user` WHERE `id` = ? "); $statement->execute([$userId]); $userData = $statement->fetch()[0]; if (!$userData) { HttpResponse::redirect('/login'); } // }
namespace Meeting\Registration; final class RegistrationController { public function register() {
$user = UserService::getCurrentUser(); if (!$user->isAuthenticated()) { HttpResponse::redirect('/login'); } // }
namespace User; final class UserService { public static function getCurrentUser():
User { $userId = $_SESSION['userId'] ?? null; if ($userId === null) { return User::guest(); } $pdo = new \PDO('localhost'); $statement = $pdo->prepare(" SELECT * FROM `user` WHERE `id` = ? "); $statement->execute([$userId]); $userData = $statement->fetch()[0]; return $userData ? User::populate($userData); : User::guest(); }
User is now a bounded context
final class RegistrationController { public function register() { $user =
UserService::getCurrentUser(); if (!$user->isAuthenticated()) { HttpResponse::redirect('/login'); } $form = $this->getRegistrationForm(); $data = $form->getData(); if (!$data['meetingId'] || !$data['ticketId'] || !$data['remark']) { return $form; } $statement = $pdo->prepare(" UPDATE `tickets` SET `sold` = 1 WHERE WHERE `id` = ? "); $statement->execute([ $data['ticketId'] ]); if ($statement->rowCount() !== 1) { return 'There are no tickets available'; } $statement = $pdo->prepare(" INSERT INTO `attendee` SET `user_id` = ?, `first_name` = ?, `last_name` = ?, `meeting_id` = ?, `ticket_id` = ?, `remark` = ?, "); $statement->execute([ $userData['id'], $userData['firstName'], $userData['name'], $data['meetingId'], $data['ticketId'], $data['remark'], ]); /* * send confirmation stuff */ \HttpResponse::redirect('/'); } }
But wait, there is more!
public function register() { // $form = $this->getRegistrationForm(); $data =
$form->getData(); if (!$data['meetingId'] || !$data['ticketId'] || !$data['remark']) { return $form; } $statement = $pdo->prepare(" /* */ "); $statement->execute([ $data['ticketId'] ]); if ($statement->rowCount() !== 1) { return 'There are no tickets available'; } // }
public function register() { // $form = $this->getRegistrationForm(); $data =
$form->getData(); if (!$data['meetingId'] || !$data['ticketId'] || !$data['remark']) { return $form; } try { $ticket = TicketService::purchase($data['ticketId']); } catch (CouldNotPurchaseTicket $e) { return 'There are no tickets available'; } // }
final class TicketService { public static function purchase(int $ticketId): Ticket
{ $connection = DB::getConnection(); $statement = $connection->prepare(" /* */ "); $statement->execute([ $data['ticketId'] ]); if ($statement->rowCount() !== 1) { throw CouldNotPurchaseTicket::becauseNoTicketsLef(); } return new Ticket($ticketId); } }
public function register() { // $statement = $pdo->prepare(" /* */
"); $statement->execute([ $user->getId(), $user->getFirstName(), $user->getName(), $data['meetingId'], $data['ticketId'], $data['remark'], ]); // }
public function register() { // $attendee = new Attendee($user->getId(), $user->getFirstName(),
$user->getName()); $registration = new Registration($data['meetingId'], $attendee, $ticket, $data['remark']); RegistrationService::register($registration); /* * send confirmation stuff */ HttpResponse::redirect('/'); }
Modules sent messages
final class RegistrationService { public static function register(Regitstation $registration): void
{ $repository = self::getRepository(); $repository->save($registration); $registrationCreated = new RegistrationCreated($registration); foreach (self::getListenerProvider()->getListenersForEvent($registrationCreated) as $listener) { $listener->handle($registrationCreated); } } }
final class NotifyAttendee { public function handle(RegistrationCreated $registrationCreated): void {
$registration = $registrationCreated->getRegistration(); mail( $registration->getEmailAddressAsString(), ‘See you soon’, sprintf( ‘Hi %s<br /> See you soon at %s ‘, $registration->getFirstName(), $registration->getMeetingName() ); } }
final class RegistrationController { public function register() { $user =
UserService::getCurrentUser(); if (!$user->isAuthenticated()) { HttpResponse::redirect('/login'); } $form = $this->getRegistrationForm(); $data = $form->getData(); if (!$data['meetingId'] || !$data['ticketId'] || !$data['remark']) { return $form; } try { $ticket = TicketService::purchase($data['ticketId']); } catch (CouldNotPurchaseTicket $e) { return 'There are no tickets available'; } $attendee = new Attendee($user->getId(), $user->getFirstName(), $user->getName()); $registration = new Registration($data['meetingId'], $attendee, $ticket, $data['remark']); RegistrationService::register($registration); HttpResponse::redirect('/'); } }
But wait, there is more!
final class NotifyAttendee { public function handle(RegistrationCreated $registrationCreated): void {
$registration = $registrationCreated->getRegistration(); mail( $registration->getEmailAddressAsString(), ‘See you soon’, sprintf( ‘Hi %s<br /> See you soon at %s ‘, $registration->getFirstName(), $registration->getMeetingName() ); } }
final class NotifyAttendeeByMandrill { public function handle(RegistrationCreated $registrationCreated): void {
$registration = $registrationCreated->getRegistration(); $mandrill new Mandrill('/* */'); $mandrillMessage = [ /* */ ]; try { $mandrill->messages->send($mandrillMessage); } catch(Mandrill_Error $e) { // } }
Oh no! The service is down
final class NotifyAttendeeJob { public function __construct(AsyncMessageBus $bus) { $this->bus
= $bus; } public function handle(RegistrationCreated $registrationCreated): void { $registration = $registrationCreated->getRegistration(); $message new SendConfimationMessage($registration); $this->bus->send($message); }
final class SendConfimationMessageWorker { public function handle(SendConfimationMessage $message): void {
$mandrill new Mandrill('/* */'); $mandrillMessage = [ /* */ ]; try { $mandrill->messages->send($mandrillMessage); $mandrillMessage->markSend(); } catch(Mandrill_Error $e) { // } }
Takeaways Locate and isolate Bounded contexts and turn them into
modules Let the world know what changed in an Event Driven plugin architecture
Joop Lammerts Website: www.procurios.com Twitter: @jlammerts