Redis and Memorystore as a session handler in App Engine

If you plan to run your web application in Google Cloud using App Engine, there was one thing that I had to solve before I could launch. That was how to share my sessions across my instances in App Engine. I created a login function based on PHP and MySQL and my user information can be used on multiple pages by using sessions. The problem is when I request something from the other instance in App Engine, the session doesn’t exist. The solution was to set up Redis and Memorystore as a session handler in App Engine in Google Cloud.

Google’s documentation suggests solving this issue with Cloud SQL. In this article, I will show you how to solve it with Redis and Memorystore. The reason why I have chosen Redis is that it works with PHP right out of the box.

Using Redis as a session handler in App Engine

One way to get around solving sessions across instances in App Engine is to install at Redis database server as a compute engine. You simply spin up a Linux instance and install Redis. Here is a guide on how to do that.

Connect to your instance via SSH and type in these commands.

sudo add-apt-repository ppa:chris-lea/redis-server
sudo apt update
sudo apt install redis-server

This will install Redis on to your Linux instance.

Test your installation by typing the following command:

redis-cli ping

This will give you a PONG response.

Now you need to allow connections to the Redis server. By default, it only accepts connection from localhost. You need to bind the IP that your web app is going to communicate with. To do that you need to modify the Redis configuration file. Type in this command to open the configuration file.

sudo vi /etc/redis/redis.conf

Look for this line.

bind localhost

Add your IP at the end so it looks like this:

bind localhost xxx.xxx.xxx.xxx

Save the file and exit.

You also need to set a password. Open the same file again and look for this line:

requirepass

Modify the line by adding your password at the end so it looks like this:

requirepass yoursupersecretpassword

Save the file and exit.

Restart the Redis service with this command:

sudo service redis-server restart

You can test your Redis installation with the following commands.

Connect to the Redis service
redis-cli -h xxx.xxx.xxx.xxx

Authenticate to the Redis Service
AUTH yoursupersecretpassword

check for keys
keys *

Right now, you do not have any keys, because you have not configured your web app to use Redis. To do that you need to do three things.

1. Create a php.ini file and put it in your web application’s root folder. In the php.ini file, you need to add the following lines:

extension=redis.so
session.save_handler = redis
session.save_path = "tcp://xxx.xxx.xxx.xxx:6379?auth=yoursupersecretpassword"

2. Create a firewall rule that allows traffic on TCP port 6379 to your Redis server.
3. Add the network, where your Redis server is located, to your app.yaml file.

You need to add this to your YAML file:

network:
   name: YOUR_NETWORK_NAME
   subnetwork_name: YOUR_SUBNET
   session_affinity: true

Using Memorystore as a session handler in App Engine

session handler in App Engine
Creating a Memorystore instance

Google Cloud comes with a session handler right out of the box. It is called Memorystore. Memorystore creates a Redis instance for you do not need to do any configuration at all. You can find the Memorystore in the menu in the Google Cloud Console under Storage. The first time you access Memorystore, the Redis API is will be enabled for you. Once that is done, you will be able to create your Redis instance.

When you create an instance, you need to configure the following:

  • Instance ID
  • Location
  • Authorized network

You also need to configure the authorized network in your YAML file for your web application. Otherwise, they won’t be able to communicate. Add this to your YAML file:

network:
   name: YOUR_NETWORK_NAME
   subnetwork_name: YOUR_SUBNET
   session_affinity: true

You also need to create a php.ini file in your web application’s root folder with the following lines:

extension=redis.so
session.save_handler = redis
session.save_path = "tcp://xxx.xxx.xxx.xxx:6379"

Put in your Memorystore instance’s IP address instead of xxx.xxx.xxx.xxx

Once that is done, you are ready to go.

Conclusion

There are two ways to deal with session handler in App Engine. The easiest way is to use the Memorystore solution in Google Cloud. It takes the least effort and it is very easy to set up and get it to work.

Leave a Reply

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