Enrico Zimuel – Develop microservices in PHP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you have a good lunch almost they're already let's let's try again are you ready okay now let's go so my name is Enrico Zimmer I work for elastic I'm a software engineer and elastic I'm old open-source contributor I did some framework expressive app agility and now I am the maintainer of elasticsearch PHP the client to call elasticsearch I'm an international speaker and actually I'm very happy to announce this this talk is my talk number 100 so thank you I am very excited to be here in Barcelona for this amou I'm also a member of the Linux Foundation for the la Mina's project I will talk a little bit later and I the co-founder of the PHP user group of Torino because I come from Italy so may recognize from my absence okay so I would like to talk about micro services this is a kind of buds world that everyone is talking about so let's have a formal introduction to micro services I just took the definition for a marketing folder so micro services architecture style is an approach for developing a single application as a suite of small services each running on its own process and communicating with lightweight Megan Eames often has HTTP so that is the former you know definition that means basically nothing you have just to split an application into small application that's the idea so for instance this is a monolithic application as we had a lot of this example in the PHP world this can be like uber you know kind of application when people can you know call taxi cars ever share and stuff like that as you can see there is this big green application in the center that managed everything so you can think of one a PHP application then manage you know the entire workflow that expose API whatever the idea is to have migrated from this architecture to this one so basically to divide to split the services into monolithic simple monolithic application so in starting from a big one we want to try to divide the application into small services should be micro because the definition is micro services but can be also small services it's just a definition is depending on you depending on the use cases of course so what are the advantages actually of this kind of approach first of all the separation of concern if you are able to split your application into independent services you are just you know have a separation of concerns so you can even build with different technology with different team with different people modularity of course because we are building services using HTTP it's very modular you can you know plug-and-play change whatever you want encapsulation everything is in one service so if you think about docker you know container is a good example of encapsulation everything is there is separated from the outside so it's quite easy to manage some time scalability is also one of the main advantages for micro services if you need you know more power more you have more traffic you can just add as many services you want and you can scale in a horizontal way of course you can also have workload partitioning so you can decided how to you know traffic how to manage the traffic for instance if you have a different you know environment with different you know machines different services different power you can decide it okay 80 percent of my request goes there are 20 go down Eggman song so it's very very simple to partition and the work so these are the advantages but of course we have also disadvantages service latency if you are using a kubernetes docker you maybe you have some you can have this kind of issue debugging instead of every one monolithic application where you can use X the bug for instance just to simple debug your PHP application you have ten services so how do you are able to the bug ten services that call each other with HTTP it's kind of a mess I mean it's a different completely different approach so the new architecture challenges actually are the auto discovery imagine it if you in the example that I show you instead of have one big application that is kind of ubirr you have a small application for you know call a car to reserve for payment and this kind of stuff but you can have more services so the same services can run on a different you know host name for instance how you can develop an application that is able to decide which one to call it at front time it's a challenge it's not that simple so they have to discover it means exactly that telemetry you need to measure some way the HTTP call the request time the response the flow so you have to have some numbers around your application just to to know how how to build stuff and last but not least everything's needs to be automated if you want to do micro services you really need a continuous integration pipeline that is fully automated otherwise you if you are trying to do manually you will fail for sure so you need a very good solid environment to build micro services is not just splitting the code is more problem comes from the architecture from the environment for the services for the munication that's the real issue of micro-services is not the code okay so HTTP is basically the common layer that everyone is using on internet and even on micro-services because you know it's very convenient everyone speak HTTP everyone knows about HTTP so there is no really choice here most of the time we are exposing REST API using JSON because again this is a common standard the error management is very very important in a micro-services so how do you manage error if I do an HTTP request and something is wrong on the services how do I expose the information I do some kind 9cj zone I try to invent my standard I reply 404 every errors I mean these are the problems you know that from an architectural point of view you have to figure out my suggestion of course try to use standard for instance application problem JSON is a specification the say okay looks if everything has an error this is the format that you have to use so everyone can just consume this you know application problem JSON and everyone is happy authentication authorization is another giant topic in micro services because you can think okay I am in my intranet so I don't need really need authorization right so every everyone is trusted by the IP or whatever but you need authorization because maybe one services is not allowed to call another services I just give you this example the payment services which one should call the payment services only a few not everyone so it's important to have this authorization mega names inside your micro services architectures so sometimes people say ok but wait a minute you're saying that we have to move from you know internal call by PHP code socket whatever into HTTP we are going to fail it's too slow everyone knows that ATP is love right actually it's not so slow because we are talking about internal application so in internal networks often use one gigabit or even more just to give an example do some math imagine that you have an HTTP response of JSON and is about five kilobytes that is a lot for a JSON API right you can have 26 K response per seconds in one using one gigabit not too bad from one node to another so network connection is not usually the bottleneck it's not it's not an issue you have to forget about speed you have to think about scalability that's the approach of micro services micro services is really needed if you have scalability if you can scale your application just adding more services out of the box that's the idea it's not about speed is about scalability okay so now let's jump to PHP I give you some just you know abstract introduction let's focus on PHP because we would like to write micro services PHP right okay HDPE PHP is a as you also notice maybe yesterday from Rasmus was born using you know for web application right so how to manage HTTP message in PHP we have this not so sexy way to manage it using global variables that everyone know that is you know the evil of any good developers so in PHP if you need to have some information about the HTTP message you have to manage dollar underscore server dollar underscore post dollar underscore get dollar underscore files the underscore cookie come on we are 2019 we need to manage global variables for HTTP messages sounds crazy and in fact it's crazy there are a lot of functions in PHP also to manage SCP like HTTP response code header had a remove header list other sans set cookie set for cookie get us name hundreds of function so it's not very clear and simple to use all this function so it's a mess but likely we had a solution we had PSR 7 that is a standard a proposal standard of the PHP framework interoperability group just be fig everyone knows about this project raise your hand if you know ok almost everyone if you don't know about the brother strongly suggest to have a look I will publish the slide when you see something in blue is our link hyperlink so you can click it and check it out so basically it's a group of people mainly you know framework maintainer library maintainer they decide to have together some to share together some ideas and they propose some interfaces so in object-oriented way and they propose PSR standard recommendation one of I my opinion the most important is a pure self 7 that is basically a common interface for representing HTTP message so instead of playing with global variables and Nancy function name for getting the HTTP request and producing the HTTP response you will manage object in an object-oriented way sounds more nice right so basically it's just an interfaces so if you go to the github page for the PS l7 this is just a list of interfaces that you have to implement in your project but of course there are many libraries that already provide you the implementation so you don't have to just to give you an example we want to get an adder from a request we have an object dollar request we have a function that is get header we specify the name of that and we get the reader that's it simple clear if you want to check if a request as an there is HUS header function that's it you need query parameters get query parents simple beautiful object-oriented okay so we fix the issue with the PHP I mean there is a way to manage HTTP requests and response in an object-oriented way in catch P thanks to PS r7 now let's jump to another topics that is wall who knows wall extension presenter on a few people ok so basically is wall is an extension for PHP is a synchronous programming framework for PHP seven actually that's the definition is as an extension you have to install it using for example or you have to compile by yourself it's apache license so is you can use it even for commercial products if you're more interested this is the website of the framework the feature gives you basically an even driven and synchronous programming for PHP you can do a synchronous stuff in PHP as you do in no js' the same way it provides you a lot of protocol for managing you know TCP UDP HTTP WebSocket HTTP - it provides also the layer as a cell is I performance and scalable with very high performance I will show you some numbers and also give you a fast sterilization and answer alliteration so that basically is better than the actual implementation of PHP I mean it's more as fast and also provide you a millisecond task scheduler so this is something very very nice if you have a very high traffic website so this is the architecture as well so basically when you run as well application in PHP this wall itself create a master so basically it's a PHP instance that same memory that generate a manager it is a different thought process and the manager will generate different work so basically means your application runs for instance on this laptop I have a height course so they will generate eight workers for one core according to the machine so basically it's a way to try to use as many you know resource the machine provide you this is an example of an HTTP server so imagine that you want to write HTTP server in PHP using this wall the same idea of node so as you can see here we are just getting a new server and we are providing to function start and request start is when you deserve very started so as you can see it's just an anonymous function here we just printing this started at information and you have this other events request that is executed when our requests come in so if someone send you an HTTP request to that IP address with that port number this is the function that we executed and as you can see we are using objects dollar response header dollar response ends so we are just putting an header in the HTTP response and writing down the body that's it when you have a start here basically this world will execute this in memory and you have a real HTTP server running in a few lines of code of PHP that means you don't need any more the web server again wait wait one minute to think about this sentence you are developing PHP application without our web server this sound crazy right is more no js' stuff it's not PHP right do you agree yeah so it's completely new approach of developing PHP application using this extension just to give you some benchmarks of this simple HTTP server is able to provide on a very old machine actually 60 K requests per second 68,000 requests per second not so bad the idea behind this wall is the idea of the event loop the same of notches so there is something running in memory that lists and for specific events on the HTTP so when is the P request comes you serve the HTTP response okay I give you some you know information here I say ok this is the way that you should write micro-services we jump to the HTTP management in PHP I introduced wall but now there is the final piece I would say that is the middle were what is middle were a middle it's basically a function that gets a request and generator response in some way in the example that I showed earlier of this wall when there is des vins request this is actually a meteor you have a request a generator response right so this is something very you know theoretical imagine that you want to build an application using that approach because at the end of the game we are building web applications web application is a HTTP message come in do some stuff and Toulouse HTTP response right so is a string that manipulate a string I generate another string if you want to see in a very row away but imagine that we want to create this middleware and put together so I would like to write a function that provides you some work on the HTTP requests that pass the information to another function to do another stuff and so on imagine I don't know authentication authentication maybe is the first midler were you want to authenticate the HTTP request if you have authentication right you pass the ball to the next you know middleware and so on so imagine that you want to like a microservices actually the idea of microservice is actually very similar to the middle you need basically something that gives you the possibility to jump to the next execution right because these are just function that are called ad want each other so if we just had as a second parameter a callable that is something that i can call in a middleware this is very powerful because this gives you the ability to say okay I do something with the request and I call the next middle work it's a way to create a pipeline actually of execution so with this simple solution just adding a parameter delegate we can actually create an entire application using this approach the idea is the same of the kernel of Linux you have this onion architecture where you have this application that is built using different mid there were and you call and you delegate the execution to the other middle-earth just give you an example imagine that you want to build a caching system for a middleware applications so typically a caching system is something that is at the top of execution when an HTTP request comes I need to check if I already have the response in the cache and if I have it I just return the response otherwise I generate a response so I pass I delegate the execution to the other mid over what I described is exactly that if cache imagine that dollar cash is some variable I don't know memcache whatever you want has requests so there is a function is okay I have this cache in my box or not I just get the response and the return X so the execution basically ends on this line if the cache the request is not in the cache I need to actually generate the response and how I generate the response just calling the delegate whatever is calling after so I call delegate passing the request I stove the response I store it in the cache and natural response so this line dollar cache set is important because the next time I will consume the cash instead of doing all the requests so with this simple approach very very basic very very simple you can build very very powerful architecture so managing an HTP requests them producing energy bills possibly using a middleware key texture it is very very simple it's very very intuitive and actually this code is not very far from the actual code for instance there were four than the expressive cache there is a library that do cache for expressive it's very very similar so the beauty of this architecture is the simplicity simple is beautiful especially if you have a full-time job on software engineering so I mentioned GSR 7 we have another standard it's a PSR 15 that basically is a way to manage the pipeline of execution of a meter work it's just an interface so basically using PS f7 and PSR 15 we can build standard mid there were application in PHP that everyone that is using the PHP fig initiative can consume and there are many frameworks they use this approach just to mention one expressive expressive is a framework that me and Mathieu and other contributors or build some years ago based on this idea of the meter world so basically is a light framework if you want to call it a micro framework that is able to give you all the feature of middle-earth it's actually a new project I mean it's come from axiom framework so last year we we announced that you know gem framework will migrate into another project driven by the Linux Foundation that is laminas so we changed the name for you know legal implication so now the project is under the Linux Foundation umbrella and actually this is the only PHP projects managed by the rooms from the is quite awesome to have you know a PHP representative in the Linux Foundation and expressive is so now part of laminas we are still in the process of changing the name space because every project under that same framework umbrella was Zend we started with Zen so we need to change in laminas but as you can imagine we cannot change easily you cannot say ok starting from tomorrow everyone the same project will have laminas as first name space we basically broke all the application of existing application so what we are doing now is to providing you automatic tools in order to change in your existing application the namespace it is quite tricky I mean there a lot of use cases a lot of very very interesting you know application legacy application even that we have to match anyway we are very close so with this tool will be just a way to install composer this component and you will have the new namespace in LA - let's say expressive support of course yes of 7 and PS r15 the main fish are the routing the dependency injection templating and handling so basically we provide a way to root easily route the HTTP requests into your code everything is dependency injection so we try to apply in our the best practice that we learn after many years we provide them plating and heroine dream this is just an example few lines of code you have this dollar app object application and you say ok look I want to manage our get HTTP requests we'd get with the puffy slash api's - ping and when someone send a get to this URL you basically call this simple function anonymous function that provides you a JSON response JSON response is a class that we provide an expressive in order to have our JSON response you pass an associative array and that's it of course this is just a toy example in releases production system bill you don't use you know anonymous function you want to use a class that implements the kids r15 interface they provide you the response but the idea is the same so instead of as a second parameter as you can see you can even specify the name of the class that implements the interface for managing the the module and that's it so this is pinger angular and this is the pinga under class of course there are some you know namespace but the only function that you have to implement is actually this one and all that is is r15 interface so as you can see handle as just one parameter that is the request and produce a response and you just pass the same jason response so this example and that one are the same basically but of course you have a class here so you can do other stuff imagine if you want to call my my sequel of database application you can pass in constructor the dependency you will consume the model the services and so on so i mean the normal workflow of all the framework we provide a lot of way to specify routing actually i just show a very simple example but you can even be more creative you can have of course optional parameter in url for instance in this example ID is an optional parameter so this is kind of a rest api or you pass or not the idea if you don't pass the ID you get all the list of the user if you pass the ID you manage the specific user and as you can see there is a as a second parameter an array because here you can pass as many middleware you want and they are executed in order so the first one is the authentication because you want to authenticate this HTTP request if the authentication is okay you pass to the second method that is authorization so you want to be sure that the one this has their position as can actually call the other meter world and the last is the action itself as you can see we can also you can also specify the HTTP methods that you want to implement the last parameter is just the name of the route you can also use direct if you don't want to use this approach you can just use get post patch delete whatever name function so very very simple approach okay so I introduced wall and through the expressive let's try to combine together let's try to use wall as a runtime for an expressive application it's very simple we provide this send expresses wall so you just require using composer in your application and you you don't have to do nothing your expressive application will run automatically on this wall how I can do that you have to start of course the application and we provide a script that is then expresses wall with the start parameters imagine that is like you know our normal services in a Linux box you have my sequel service start stop or restart whatever the same area because actually this is a service is something that is running in memory we are not using a web server anymore right so this is just a simple script that runs in memory so you have PHP we are expressive we have all this is kind of a revolution of our PHP developer because you are running web application using the common line interface like nodejs and you seem provided the deployment because you not need to manage anymore a webserver for instance you can adjust one container in docker with PHP and that's it that's the only technology that you need in order to run a web application or an API or whatever you want to call it it's quite awesome write some benchmark I promise you some exciting benchmark here the results with this approach a normative application is from two to four times faster than nginx and Apache okay you got it this is just you know a benchmark that I did some actually years ago so maybe now I have to redo again just to see now actually is wall is a full-time project for at least two employees so they are doing a lot of nice our Chinese developer the Chinese community in the last year is very very involved in PHP project I don't know why I don't know if there are some reason for that but it's a fact maybe there are a lot of people so you know it's good to have the help of such good developers okay but again I started my presentation saying that I micro services is more than just code is more than just an API if you really want to do micro services you need to manage the environment so basically the architecture the entire architecture basically you need for instance logging you need to aggregate the logging so all the logging produces by all the services should be in one place in one databases you cannot have you know file split into containers services whatever and you try to grab the information this is a giant log file it's impossible absolutely impossible so you have a concrete way to aggregate the logic you have a an instruments for monitoring the application so you have to get some telemetry some metrics behind your application you need to know how many you know millisecond this HTTP response is polluted by this HTTP request how many memories are located by the services the CPU usage and so on and you need also an application performance monitoring for the backing because as I said you cannot use X the bug for a micro services I mean you can use for a single you know services but if you have to debug you know an application that call another application that they call another application and so on you cannot do that and actually you need more you need a way even to track this jump from one service to another and this is actually distributed tracing so distributed tracing is just a piece of information that lives in the HTTP request an ID basically that is present in all the following requests so if you have this link basically this ID and if you have a system that is able to collect all these information you can actually trace your application say okay this is the services that call this and other services because because you have this ID this reference so it's very very important to have this rebooted pressing in micro services and there is a proposal for standard but or actually a standard the fact that it is you know open tracing for instance you can check it out if you are if you're curious so basically this idea of micro services lives under this name that is observability so you need a way to observeit the running of your entire application entire micro services so you need lots you need metrics unit phrase and this were elasticsearch can help you elasticsearch is a database actually the ice caliber open source full-text search and analytics engine this is the definition that is very fast I mean if you try to use lastic search for you know almost near real-time application a lot of people are using it even with you know trillion of records trillion of documents it's very very fast so it's designed to be fast for the search and this is very important because if you have a micro service application you want if you add for instance one to debug an application you don't want to spend time building a query to search your database and wait seconds for the response you need something that is you know give you the the answer just in time it's the silent for scale horizontally so you can if you need more power you can just had another instance of elasticsearch pooling in the cluster and that's it is automatically you know balance between between the nodes and last but not least is REST API so the beauty of this approach is everything all the feature of elastic search and actually all the feature of the elastic products are just a REST API so you can just consume a recipe are in order to index a new document for instance to search onion documents everything is the recipe I so it's perfectly aligned in this idea of microservices example in PHP this is the library the official client for elasticsearch recipe if you want to ingest some data into elasticsearch using PHP very simple you have this client you create the object you pass the information so they index the type the body whatever and you have this index function that is a way to create an index into elasticsearch that's it a response is an associative array with all the information of the recipe I so basically this is a simple proper of the HTTP call for elasticsearch but I say it okay now we have a system you can put everything into this elastic search but how we can query the information now we can search for you know the bugging or all this kind of stuff we have another product this is also open source this is cabana this is basically a UI that is a window into the last six step it's a way to query using also diagram graph the information so it's very very simple very very intuitive to use it you have just to you know look for specific ID or specific text specific keyword token and elastic sure we provide around so it's also nice because we have an additional feature in top of the open source stack the elastic for instance in machine learning so it basically this is an automatic way to discover anomalies in elastic self data imagine you are storing all the HTTP requests and response of your application even the execution time you can have some pic right this is a at all machine learning disabled to analyze the normal behavior of your friends of your traffic and just show you if something is unexpected so maybe it's not a big deal maybe it's an attack maybe it's a bug whatever but it's an automatical to a very nice feature to have it so in this diagram we have a an overview of all the majority of the products involved in this presentation that are basically elasticsearch that is the database that collect the data Cubana that is the UI so you can interact with the Cabana and ask you for information we have also a log stash log stash is a elastic product that's open source that is able to get information from syslog from the log of I don't know web server and my sequel whatever you to ingest and put into elasticsearch and we have also a PM that is basically the application performance monitoring of elastic open source but where your application basically can send specific information to elastic search imagine that you want to debug or even track specific piece of code of your application for instance on the login may be the login page of your application you want to track in a specific you know you want to store I don't know how many login you have how many fail in the login this kind of information you can actually using a p.m. say okay look this code starting from this piece of code to that one so you have to basically put some place order start and hand into your code and basically the API imagined will just get this piece of code taking the execution time or all information you want to store it and send it to elasticsearch this is just a representation of the APM so basically APM using cabana is just a specific view of your code that you are ingesting and as you can see we have this transaction sample this is very very important for a micro services approach because here is where the distributed tracing happens because as you can see here we have two bars in blue and other in green actually this green are another services that is colored by the blue one and you can see there even the parallel execution say okay this time here i call another services here that is the the green one but i still using the blue one so this is just a way to show how the application is running how you can you know fix stuff how many requests how many execution time you are spending and so on if you don't have this kind of information is very very hard to build a microservices application okay a peon in PHP I say that we have this agent actually even here we have a recipe i if you are interested if you want to see how ingest data into APM and also in elasticsearch we have this intake api so it's a public document everyone can can use it to ingest data into a PM server we have also a community agent provided by one contributor this is the github name for the project and the good news is that we are working on an official APM agent for PHP I mean elastic is working on IBM agent for PHP and this will be an extension so PHP extension because of course we want to provide you an automatic way you install the extension you don't have to do nothing you get all the information from you know execution time and relocation and so on and also a library I think it's important to mention that we provide both because the library is where you if you want to have outer instrumentation you can have the extension but if you want to insulate piece of code in your code base you have to use a library and specify just the start and the end in the points that you want to monitor that's it okay so I finished my presentation I don't know if there are questions thank you thanks so much okay I have to say muchas gracias right my Spanish is horrible even if I'm Italian sorry so we've had before a Tolkien react PHP so I guess the question is obvious how does small compared to react PHP differences and things like that okay so basically as well is more you know an extension rather than react HP is a library that provides you you know using the actual API of PHP and a synchronous framework as well is more than that is not just a synchronous framework it provides you a lot of other other feature like I didn't mention for instance the core routine is a way very simple to provide a synchronous approach is the user lingo for instance if you are familiar with God you have this core routine and you can have the same in PHP so it's more than on a synchronous framework and people are asking if schwa is ready for production or if you are aware I guess a client using it already at least to client to customer actually ex customer of Zen that use it and it's again in China is very very very popular a lot of big social network even are using it so I don't know the name not ask me the name because I'm not able to time so I'm staying for a while we'll do two more questions but around if you have any question about micro services what is your thought about like when whenever you have a chain of cold or like micro service that depend on each other could you be up for all API HTTP requests or like versus the approach of sending events to perhaps some private mq and process that like kind of a synchronously the distributed tracing is almost this one I mean if you have a same ID in the all the other of the HTTP chain and on instruments like elastic for instance it's quite easy to collect all this information and provides you in a dashboard to say okay this was the call and debugging normal application so it's important to collect but also to be able to query the information that wise is like data backup and restore you do the backup but did you test the rest or who test the restore when you need it doesn't work and a final kind of philosophical one someone in well and actually custom like so if we are killing maybe perhaps were killing domain-driven design some of their patents with these new well the new bat-mite middle way approach that it's emerging now I don't know it's different level this is more architecture than methodology for you know development and we know there are like million of questions more but we only have time for one more if you want in the audience no ok well played to check with and break up will be around for a while and thank you and Rico you
Info
Channel: PHP Barcelona
Views: 11,935
Rating: undefined out of 5
Keywords: php, microservices, elastic, kibana, elk, barcelona
Id: FXo85zItAzg
Channel Id: undefined
Length: 46min 48sec (2808 seconds)
Published: Thu Nov 28 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.