Why reactive application?

11. Mai 2016

0 Kommentare

Application requirements have changed dramatically in recent years. Only a few years ago a large application had tens of servers, seconds of response time, hours of offline maintenance and gigabytes of data.

Today applications are deployed on everything from mobile devices to cloud-based clusters running thousands of multi-core processors. Users expect millisecond response times and 100% uptime. Data is measured in Petabytes. Today’s demands are simply not met by yesterday’s software architectures.

Traditional applications

reactive-api-with-php_traditional_

Each request is using 2 running processes, one for each instance of HTTP server and database server. The PHP framework might also generate its own additional process.

The HTTP server instance is generating a request object, which is mostly overloaded for later usage.

The database instance is blocking the response until it has finished its task.

The database response is being buffered by the PHP process in order to generate the DTO (data transfer object).

The PHP process is using the DTO to generate a JSON response for the HTTP server instance.

The entire process starting with the HTTP request until serving the response back to the client, is running in the same thread. This uses the CPU’s default behaviour, which leads to an unbalanced usage of CPU cores.

cpu_traditional_application

Reactive applications

We believe that all necessary aspects are already recognised individually: we want systems that are responsive, resilient, elastic and message driven. We call these Reactive Systems.

reactive-api-with-php_reactive

There is no need for a HTTP server. The application is using one single process. PHP and the database (SQLite) are running inside this process.

There is no need for an overloaded HTTP request object. The required data is mostly limited to request method, path and payload.

The response can be sent immediately to the client, while the data is being processed in the background and all necessary steps are being handled in an asynchronuous manner, using events.

The database is not blocking the response, as its task is being handled asynchronuously.

The entire process is happening within one persistent TCP socket, enabling the repsonse to be streamed to the client.

The process is managing its tasks with several threads, balancing the usage of all CPU cores.

cpu_reactive_application

Example Implementation

As an implementation example we used lightweight components which we packed in Docker containers. AlpineOS and Ubuntu already come as Docker images ready to be used.

ReactPHP is responsible for establishing the TCP socket, transferring the raw HTTP request and sending the HTTP response back.

PIMF micro framework is responsible for managing the HTTP resources using the HTTP request methods, validating the data and handling the persistence interaction.

reactive-api-with-php_example

You can find the source code at GitHub: https://github.com/gjerokrsteski/reactphp-pimf

Feel free to fork it and experiment yourself!

Conclusion

Systems built as Reactive Systems are more flexible, loosely-coupled and scalable. This makes them easier to develop and amenable to change. They are significantly more tolerant of failure and when failure does occur they meet it with elegance rather than disaster. Reactive Systems are highly responsive, giving users effective interactive feedback.

Resources:

The Reactive Manifesto
http://www.reactivemanifesto.org

What is Reactive Programming?
https://medium.com/reactive-programming/what-is-reactive-programming-bc9fa7f4a7fc#.j5m4cnvck

ReactPHP
http://reactphp.org

PIMF
http://pimf-framework.de

Docker
https://docs.docker.com

This is the result of an Exploration Day exercise by Gjero Krsteski and Ralph Bach.

¬ geschrieben von gjerokrsteski in News und Trends, PHP Tricks und Tipps, PIMF  ¬ Erzähl´s weiter Twitter  | Mr. Wong  | Delicious  | Del.icio.us  | Google  | Facebook

Checkout PIMF Starter Book

28. April 2016

0 Kommentare

pimf-php-micro-framework
This is a hands-on book. You won’t be able to complete it by reading it in a metro on a way to work. You’ll have to read this book while in front of a computer getting your hands dirty. You will learn developing console and web applications as well as micro services. This book is interesting for intermediate developers as well as beginners.

Learning PIMF

One of the best ways to learn PIMF is to read through the entire of its documentation. This guide details all aspects of the framework and how to apply them to your application. http://docs.pimf-framework.de

Read the PIMF Starter book almost anywhere. Available as a PDF, EPUB and MOBI. You can now read it on all devices, as well as offline: http://book.pimf-framework.de

Eventually, you might get stuck and in need of help. Or you might want to write a review or comment on the book’s content. Please post your thoughts on this blog post. If you prefer one-on-one discussion, feel free to send me an email to gjero@krsteski.de, and I’ll give my best to help you out.

¬ geschrieben von gjerokrsteski in News und Trends, PIMF  ¬ Erzähl´s weiter Twitter  | Mr. Wong  | Delicious  | Del.icio.us  | Google  | Facebook

PIMF vs. Silex

27. November 2015

0 Kommentare

This performance comparison was done by TechEmpower, Inc. CA. They made the benchmark of many web application frameworks executing fundamental tasks such as JSON serialization, database access, and server-side template composition. Each framework is operating in a realistic production configuration. Results are captured on physical hardware. The test implementations are largely community-contributed and all source is available at the GitHub repository.

In the following tests, TechEmpower have measured the performance of several web application platforms, full-stack frameworks, and micro-frameworks (collectively, „frameworks“). For more information, read the introduction, motivation, and latest environment details.

Hardware

Peak: Dell R720xd dual Xeon E5-2660 v2 (40 HT cores) with 32 GB memory; database servers equipped with SSDs in RAID; switched 10-gigabit Ethernet

Platform

Current versions of: Linux, PHP-FPM, nginx on Linux, MySQL on Linux

Single query test

In this test, each request is processed by fetching a single row from a simple database table. That row is then serialized as a JSON response.

Best (bar chart)
Single query Best (bar chart)

Data table
Single query Data table

Latency
Single query Latency

Framework overhead
Single query Framework overhead

Multiple queries test

In this test, each request is processed by fetching multiple rows from a simple database table and serializing these rows as a JSON response. The test is run multiple times: testing 1, 5, 10, 15, and 20 queries per request. All tests are run at 256 concurrency.

20-queries (bar)
Multiple queries 20-queries (bar)

Data table
Multiple queries Data table

Latency
Multiple queries Latency

Framework overhead
Multiple queries Framework overhead

¬ geschrieben von gjerokrsteski in PIMF  ¬ Erzähl´s weiter Twitter  | Mr. Wong  | Delicious  | Del.icio.us  | Google  | Facebook

Should a function have only one return statement?

13. August 2015

9 Kommentare

I would say that you can definitely have more than one return statement. Therefore I would like to suggest to:

1.) Minimize the number of returns in each routine. It’s harder to understand a routine if, reading it at the bottom, you’re unaware of the possibility that it returned somewhere above.

2.)Use a return when it enhances readability. In certain routines, once you know the answer, you want to return it to the calling routine immediately. If the routine is defined in such a way that it doesn’t require any cleanup, not returning immediately means that you have to write more code.

Question: Which of the functions shown below would you prefer and why?

Version 1:

function getTemplateId(array $config, $id)
{
    if (array_key_exists($id, $config)) {
        $templateId = $config[$id];
    } else {
        $templateId = 0;
    }

    return $templateId;
}

Version 2:

function getTemplateId(array $config, $id)
{
    if (array_key_exists($id, $config)) {
        return $config[$id];
    }

    return 0;
}

Please write your answer as a comment :-)

¬ geschrieben von gjerokrsteski in PHP Tricks und Tipps  ¬ Erzähl´s weiter Twitter  | Mr. Wong  | Delicious  | Del.icio.us  | Google  | Facebook

Powered by Wordpress • Abonniere den RSS Feed