We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Phalcon 4.x Sessions Via MySQL?

I've looked at the incubator repo on GitHub for the MySQL session adapter and it seems that it's not up to date with Phalcon 4.x in terms of getting sessions logged within MySQL rather than relying on the Stream adapter to store sessions via files.

I have an app that is starting to get massive traffic and while I'm able to spin up more servers in a load balancer, it becomes an issue keeping the session files synced between multiple servers. If a user on server A gets logged in and then is kicked to server B because of load balancing, they aren't logged in anymore and have to re-create their session.

I'm trying to figure out either a.) what needs to be done to get that incubator script up to date, b.) need an already up to date adapter that someone knows of or c.) how exactly to create my own adapter to properly work with the session adapter interface.

I've been google searching this issue for weeks now and I find it weird nobody has found a solution yet. Maybe I'm missing a thread that got buried?

Any help would be greatly appreciated.

P.S. - On a side note, is there any way with the Stream Adapter for sessions to be set to a time limit? I'm having trouble finding how to do that in the docs.

edited Aug '20

You can use memcached locally sharing it to your servers behind the load balancer.

You can use memcached locally sharing it to your servers behind the load balancer.

Thanks for the suggestion! Unfortunately, because we have developers working on local copies of the website (across Mac, Windows and Linux) and pushing their work through GitHub for review and deployment, adding on to their plate the need to setup and run memcache as part of their local environment would be extremely cumbersome.

The ideal situation would be to get sessions stored via MySQL, as their environments already have that in place and because we plan to leverage session data within the database for analytics and support for users within the app.

I've been trying to wrap my head around the docs regarding making a custom adapter for sessions in Phalcon 4.x, and it seems that the main change was the naming convention. Am I correct in assuming that if I update the class names and paths in the incubator script to use the correct namespaces in Phalcon 4.x that it should be smooth sailing from there?

Yes but you should use it only in production and in developpement stay with the stream adapter, your developers doesn't need to share their sessions.
I can't help you about custom adapter, I nerver used it, but maybe namespace update is enough.


if there is no sensitive data why not use jwt

edited Dec '20

I worked on a company (adult industry) which handled 1B requests/day... that's 12k req/sec. We used Symfony2 with its standard distribution, with a few tweaks for our use case. Our infrastructure also used Riak for cache storage, RabbitMQ for message broker and Doctrine2 ORM at the top of MySQL for primary storage. So ya... ORMs do perform and ya... a potato can handle your load (as VeonIndigo Cardiksaid).

Indigo Card

edited Dec '20

For anyone who is searching via Google, I found the solution after digging around quite a bit.

First, it seems that the documentation at https://docs.phalcon.io/4.0/en/session#custom is not correct, as it is explaining you need to write an adapter that extends SessionHandlerInterface, but as you can only implement interfaces, this leads you down a dead end. Instead, you need to write a class that extends Phalcon\Session\Adapter\AbstractAdapter.

Second, I wasn't aware that with Phalcon 4.x, the team decided to separate their incubator library into different repos, so if you're on Phalcon 4.x and looking for the MySQL database adapter for sessions, it is now located at https://github.com/phalcon/incubator-session.

I hope this helps those that are possibly still looking for a solution.