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.

Table Of Content

  1. Amazon DynamoDB
  2. Install AWS PHP SDK
  3. Building CakePHP DynamoDB Session Handler
  4. Activate DynanoDbSessionHandler
  5. The end
1. Amazon DynamoDB

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.

2. Install AWS PHP SDK

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:

3. Building CakePHP DynamoDB Session Handler

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.

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:

Supply your Amazon credentials accordingly. As for session_table's value, use the one you created in Step 1 (Amazon DynamoDB).

4. Activate DynanoDbSessionHandler

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:

Now our CakePHP application's session should be working with Amazon DynamoDB.

5. The End

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 .