How to scale a PHP Application?

How to scale a PHP Application?

How to scale a PHP Application? This article gave a good overview of the different components needed to scale a web application. The easiest way to do it is to use a cloud provider and let them handle the scaling for you!

PHP gets a lot of hate in my experience. So much hate that I’m almost afraid to say that I like the language. Version 7 solved a lot of issues and I specifically love the type-checking features that were added.

And, if you use the popular Laravel framework, all the ugly parts of PHP are hidden away and you can work with the beautiful libraries Laravel provides for you.

One argument that comes up a lot when I talk about PHP is scalability. There seems to be the notion that PHP applications don’t scale.

In this article, I will show you what is needed to scale up a PHP application.

Vertical Scaling

By far the easiest way to scale up your application is to buy a better server. Adding more CPUs and RAM will speed up your website, but it comes at a cost as buying a high-end server is expensive.

Furthermore, it is hard to dynamically scale your application this way. Suppose you have almost no users at night and around a million users during the day, then you have a very expensive server that you only use half of the time.

This method of scaling is called vertical scaling.

Horizontal Scaling

Almost all big web applications use horizontal scaling. This means adding more servers to scale up the website — instead of buying a better server.

Typically, there is a load balancer that will redirect users to one of the application servers.

It is important to separate the database from the application servers. That way, each application server will have access to the same database.

PHP Sessions

There is a problem with this method though. The server must be stateless.

PHP uses sessions to keep track of the user’s state. If a user logs in on Server 01 then that server knows the user is logged in. But if the load-balancer sends the next request to Server 02 then this server will not know that the user is logged in.

Solution 1. Store the session in the database

You can configure PHP sessions to store session data in the database.

However, this means that for each request, a database query must be made. This scales badly as most often the database is the performance bottleneck in large web applications.

Solution 2. Store the sessions in a Redis server

Redis is an extremely fast in-memory key-value data store. You can set up a Redis server that will store all PHP session data. Check out this article for a practical guide on how to do this.

Solution 3. Decouple your app into a back end and front end

You could choose to not use PHP sessions at all. A lot of websites have a stateless API as back end and use JSON web tokens as an authentication system.

This is the most scalable solution, in my opinion, as all the session management is done on the client's side.

Adding More Databases

Most applications perform more read queries than write queries. You can use this information to scale up your databases by adding read-only databases.

To keep the data consistent, these read-only databases sync with a single write-only database.

If you need to add more write-only databases, you enter the land of database sharding, which is very complicated and I would not recommend doing yourself. Check out this article if you want to learn more about sharding.

Handling Uploaded Files

If a user uploads a file to Server 01 then Server 02 will also need to know that the file was uploaded.

A solution could be to use Gluster. Gluster will automatically sync a filesystem over multiple nodes in a network.

Another solution could be to create a single object-storage server, the only purpose of this server is to store static files. As a bonus, you could create multiple object-storage servers and use Gluster to keep them in sync!

Cloud Computing

Successfully scaling up a web application is a lot of work. That is why most companies turn to cloud computing to scale their applications.

With the Google Cloud platform, you can spin up a Google Cloud database as your database server. Use Cloud Run to host your application server. Create a Google Storage bucket to host static files. And use Google Cloud Firestore as your key-value session server.

The amazing thing is that the Google Cloud platform will automatically dynamically scale your application. No work on your part required!

Conclusion

Hopefully, this article gave a good overview of the different components needed to scale a web application. The easiest way to do it is to use a cloud provider and let them handle the scaling for you!

But, it can definitely be fun to set up the components yourself.

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

In Laravel 6 tutorial, we learn how to use PHP artisan command interface (CLI) to clear the cache from Laravel 6.8 application. How To Clear Cache in Laravel 6.8 Application using Artisan Command Line Interface (CLI)? How to clear route cache using php artisan command? How to easily clear cache in Laravel application? How to clear config cache in PHP Laravel via artisan command? How to clear Laravel view cache? How to Reoptimized class in Laravel via artisan CLI?

Today in this tutorial, we are going to learn how to clear route cache, laravel application cache, config cache, view cache and reoptimized class in a Laravel 6.8 application using artisan command-line interface.

I’m pretty sure many of you may have found yourself gotten into the situation where you do not see changes in the view after making the changes in the app.

Laravel application serves the cached data so caching problem occurs due to the robust cache mechanism of Laravel.

But, if you are still facing this issue, then you do not have to worry further. Let me do the honour of introducing you some of the best artisan commands to remove the cache from your Laravel app via PHP artisan command line interface.

Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.

Table of Contents

  • Clear Route Cache in Laravel
  • Clear Laravel Application Cache
  • Clear Config Cache via PHP Artisan
  • Clear Laravel View Cache
  • Reoptimized Class
Clear Route Cache in Laravel

Laravel caching system also takes routes in consideration, to remove route cache in Laravel use the given below command:

php artisan route:cache
Clear Application Cache in Laravel

Run the following command to clear application cache:

php artisan cache:clear
Clear Config Cache in Laravel

Run the following command to clear config cache:

php artisan config:cache
Clear View Cache in Laravel

Run the following command to clean your view cache:

php artisan view:clear
Reoptimize Class

Run the below command to reoptimize the class loader:

php artisan optimize

Conclusion

We have completed this Laravel 6 tutorial, In this tutorial we learned how to use php artisan command to clear the cache from your Laravel application. We have answered the following questions in this article.

  • How to clear route cache using php artisan command?
  • How to easily clear cache in Laravel application?
  • How to clear config cache in PHP Laravel via artisan command?
  • How to clear Laravel view cache?
  • How to Reoptimized class in Laravel via artisan CLI?

Now, it’s your time to let me know what do you think about this laravel 6 article. Go forth and try these super awesome artisan commands and let me know how these commands are helping you.

Get Weather Data with Laravel Weather

Get Weather Data with Laravel Weather

Get Weather Data with Laravel Weather. Laravel Weather is a good package which we can use to get weather data. It's a wrapper around Open Weather Map API (Current weather). A wrapper around Open Weather Map API (Current weather)

🌤️ A wrapper around Open Weather Map API (Current weather)

Installation

You can install the package via composer:

source-shell
composer require gnahotelsolutions/laravel-weather
Usage
text-html-php
$weather = new Weather();

// Checking weather by city name
$currentWeatherInGirona = $weather->get('girona,es');

// You can use the city id, this will get you unambiguous results
$currentWeatherInGirona = $weather->find('3121456');

Units

By default the package uses metric for Celsius temperature results, this can be modified using the configuration file or on the fly:

text-html-php
$weather = new Weather();

$currentWeatherInGirona = $weather->inUnits('imperial')->get('girona,es');

Language

By default the package uses es for the description translation, this can be modified using the configuration file or on the fly:

text-html-php
$weather = new Weather();

$currentWeatherInGirona = $weather->inLanguage('en')->get('girona');

Guzzle Client Instance

If you need to use another instance of Guzzle, to modify headers for example:

text-html-php
$weather = new Weather();

$guzzle = $this->getSpecialGuzzleClient();

$currentWeatherInGirona = $weather->using($guzzle)->get('girona');

Testing

source-shell
composer test

Laravel Sweet Alert | How to use the SweetAlert package in Laravel PHP

Laravel Sweet Alert | How to use the SweetAlert package in Laravel PHP

Sweet Alert is a laravel wrapper around #sweetalert javascript library for showing alert or notification message. This is a great package created by rashid, and this laravel package is very famous because it has tonnes of features you can choose from.

Sweet Alert is a laravel wrapper around #sweetalert javascript library for showing alert or notification message.

This is a great package created by rashid, and this laravel package is very famous because it has tonnes of features you can choose from.