It requires a scalable infrastructure to run a scalable web application. The infrastructure must be able to handle unexpected traffic growth in an automatic manner. For example, when there is a sudden traffic bump, the infrastructure needs to spin up more servers instantly to cater the request.
Nowadays, cloud solution has become the preferred way to build a scalable web application. By using cloud services, we as developers are able to build scalable applications without worrying about the infrastructure behind it. It is all taken cared of by cloud solution providers. And as the pioneer of cloud solution provider, Amazon is probably one of the most popular providers if not the most.
Amazon provides the infrastructure for us to build scalable web applications. We will need to build our applications in a way that is cloud ready. The fundamental idea of cloud ready application is that our application code needs to be stateless. So that it can be deployed to any number of servers instantly without disrupting live users.
In other words, we need to split our database, storage, session store and cache store into different locations. So when we stop or start a new application server, its state will stay intact.
In this tutorial, we look at how to use DynamoDB as our session store for a CakePHP 3 application.
Amazon DynamoDB is a fast and flexible NoSQL database service. Its officially stated latency is in millisecond. Which is perfect for a session store. We will not go into the details of setup Amazon DynamoDB in this tutorial, as it is pretty straightforward and its official document explains that pretty clearly .
Please create a DynamoDB table for this tutorial purpose by following the official document at here.
Because we need to communicate with AWS DynamoDB from our application, it is much efficient to use an existing instead of building our own.
Amazon provides official support for PHP, let's install that into our CakePHP application.
Run the Composer command as shown below to install:
composer require aws/aws-sdk-php
For CakePHP 3 to communicate DynamoDB, we need to create a custom session handler. CakePHP's documentation has a detailed section on how to build a custom session handler, it is worth checking it out.
It is actually pretty easy to build the custom session handler in our case. Because Amazon's DynamoDbClient already has an API to register the handler to PHP. All we need to do is to wrap it in CakePHP style.
Copy code shown below to file src/Network/Session/DynamoDbSession.php, you will need to create the file since it is new.
namespace App\Network\Session;
use Aws\DynamoDb\DynamoDbClient;
use Cake\Core\Configure;
class DynamoDbSession implements \SessionHandlerInterface
{
private $handler;
/**
* DynamoDbSession constructor.
*/
public function __construct()
{
$client = new DynamoDbClient(Configure::read('Aws'));
$this->handler = $client->registerSessionHandler(array(
'table_name' => Configure::read('Aws.session_table')
));
}
public function close()
{
return $this->handler->close();
}
public function destroy($session_id)
{
return $this->handler->destroy($session_id);
}
public function gc($maxlifetime)
{
return $this->handler->gc($maxlifetime);
}
public function open($save_path, $session_id)
{
return $this->handler->open($save_path, $session_id);
}
public function read($session_id)
{
return $this->handler->read($session_id);
}
public function write($session_id, $session_data)
{
return $this->handler->write($session_id, $session_data);
}
}
Our DynamoDbSession implements the SessionHandlerInterface interface. In its constructor, we create a AWS DynamoDbClient object. Next we implement methods of SessionHandlerInterface interface by simply delegating it to DynamoDbClient object.
As you can see, we are getting Amazon credentials and session table's name from CakePHP Configure class. So we need to supply those first in app.php file.
Append following array key and values to app.php file:
'Aws' => [
'region' => '',
'version' => '',
'credentials' => array(
'key' => '',
'secret' => '',
),
'session_table' => 'session'
],
Supply your Amazon credentials accordingly. As for session_table's value, use the one you created in Step 1 (Amazon DynamoDB).
Final step to get DynanoDbSessionHandler to work is to enable it from app.php.
In app.php file, find the array key of Session and config it as shown below:
'Session' => [
'defaults' => 'php',
'handler' => [
'engine' => 'DynamoDbSession'
],
],
Now our CakePHP application's session should be working with Amazon DynamoDB.
Building a scalable web application is challenging, yet very exciting when you figure each piece out one by one. We hope you have enjoyed this tutorial. If you have any question, please leave a comment below, we will try our best to answer every question.
Hopefully this simple tutorial helped you with your development . If you like our post, please follow us on Twitter and help spread the word . We need your support to continue. If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know .