How to use Dragonfly to store user sessions with Symfony
Dragonfly is a modern in-memory datastore system, compatible with Redis and Memcached API, but 25x faster…
…Therefore making it a good choice if you want to upgrade your cache without rewriting any code!
Dragonfly implements novel algorithms and data structures on top of a multi-threaded, shared-nothing architecture. As a result, Dragonfly reaches x25 performance compared to Redis and supports millions of QPS on a single instance.
Let’s dive into the technical part and our case study: using it as session storage for a Symfony project.
This tutorial will be done using Docker, but if you prefer to install everything on your machine, just jump to the configuration part.
Dragonfly is not released on Docker Hub yet, so we’ll be using their private repository.
I’ll be using https://github.com/EdouardCourty/symfony-docker as a website base.
After cloning the project, run the make install
command to build the server, install the dependencies and create a database.
Navigate to http://localhost:8080
make sure the project is correctly initialized.
Let’s enter the PHP docker in order to run some commands using the Symfony CLI: make bash
Once using the container’s shell, run the bin/console make:auth
in order to create a basic login form. Choose option “Login form authenticator” when asked.
Navigate to http://localhost:8080/login
and use the credentials admin/password to log in.
An error should trigger saying that a redirection is missing in the Authenticator class. Navigate to your Authenticator and add this line in the onAuthenticationSuccess
function:
return new RedirectResponse('/login');
This will redirect you to the same login page if you enter the correct credentials, and will show “You are logged in as admin” at the top. The authentication can also be verified using the Symfony debug bar at the bottom of the page.
We are almost done! For now, all the sessions are stored using the filesystem.
We want to use Dragonfly, which is faster and easier to handle if your app grows and needs to be able to handle a lot of sessions.
We need to do three things: add a dragonfly service in our docker-compose.yml
file, install the redis
PHP extension, and configure Symfony to use Dragonfly as the session storage.
Let’s tell Docker to create a cache service by adding this entry to the services defined in docker-compose.yml
cache:
image: docker.dragonflydb.io/dragonflydb/dragonfly
Add these lines in the Dockefile to allow PHP to use the redis
extension.
RUN pecl install redis
RUN docker-php-ext-enable redis
Then simply define a CACHE-DSN
value in your .env
file:
CACHE_DSN=redis://cache
Update the config/framework.yml
file to make Symfony use our Dragonfly store as its session storage. Simply add a handler_id
to the session
config entry.
handler_id: '%env(CACHE_DSN)%'
That’s it!
Symfony will now use Dragonfly as its session storage instead of the filesystem, meaning a great performance boost for your website!
You can now log in again and start building around this starter template!