Türchen 8: Split off requests to micro services

Dec
7
2018

Magento and additional Microservices: What a dream team!

We started as probably many of you also did: with Magento and some additional extensions to add missing features and bring value to the customer. Over time it became more and more and luckily the business flourishing. By this we encountered areas in which Magento is well known: performance and easy scalability. Much was done but in the end the easiest solution disguise was chosen: Full Page Caching.
Suddenly everything seemed fine again. At least until someone had the brilliant idea of showing “more accurate/real time data” to customers. We started with a simple thing: real delivery time/availability information on product detail pages. Everything was implemented, tested and approved but after deployment nothing changed. Everybody forgot our full page cache.

AJAX hole punching

As we do not use a fpc solution that supports ESI blocks we tried the standard way of making AJAX requests for the stuff we wanted updating and it worked. Yay!! Soon after adding the availability information to the detail pages we added those to product listings, and hell broke loose. With 30 products per page we suddenly created 30 Ajax requests, all to a Magento controller. After a bit of research we found that we tried to access the user session concurrently and there is a slight problem with PHP-session locking for this which caused 15-30% of our AJAX requests to result in “503 Service not available”.

Use no user session

Somehow we were not able to implement this in the preDispatch() method as it should normally work:

As we could not figure out how to bypass this we needed to come up with another idea and we agreed upon writing our own product-availability-service.

Availability as a services

We agreed on building this one in node.js. Partially because we wanted hands on experiences in an area that was new to most of us and partially because we thought it would be a good fit for the needed features. For fast implementation and also to prevent us from reinventing everything we use express.js

Express route to a simple CRUD API

Using express.js for this is dead simple. After installing express-generator and mysql for npm for database connection you can simply generate the “project” by

This will result in a base project structure that can be used for our small example. Additionally we need to install the mysql stuff for npm:

and set it up afterwards in models/db.js

Now we can prepare the real controller for request handling. I put it in routes/stockstatus.js

Last thing we need is to tell our app how to access this endpoint. This will go to app.js where we need to add

For this example I used a very simplistic table with just a tiny bit of sample data:

This is all you need to run the application with

Normally you can access it now under http://localhost:3000/stockstatus/2 to get a JSON response.

Conclusion
As you can see it is really super simple to split off some functionalities or add new by adding a very small node.js app. Personally i would advice against enlarging those APIs without boundaries, instead I would suggest having specialized services for every need you have as in: “Do one thing, but do it right”. Those are easier to adapt, replace and scale if need be.

Kommentieren

Your email address will not be published. Required fields are marked *