FriendsOfCake / app-template

CakePHP2: An empty application template, for use with composer
71 stars 16 forks source link

App Template does not allow scaling on Heroku #57

Closed HarderWork closed 9 years ago

HarderWork commented 9 years ago

I've followed the bible on how to create a new app using composer. I enable redistogo and set environment variables as proposed.

The app gets up and running nicely on heroku when I'm running a single dyno. But when I try to scale the app (heroku ps:scale web=2) I get a lot of Internal Server Errors. Enabling debug mode and I found out that the error message is:

Fatal error: Uncaught exception 'CacheException' with message 'Cache engine default is not properly configured.' in /app/vendor/cakephp/cakephp/lib/Cake/Cache/Cache.php:181 Stack trace: #0 /app/vendor/cakephp/cakephp/lib/Cake/Cache/Cache.php(151): Cache::_buildEngine('default') #1 /app/app/Config/core.php(283): Cache::config('default', Array) #2 /app/vendor/cakephp/cakephp/lib/Cake/Core/Configure.php(72): include('/app/app/Config...') #3 /app/vendor/cakephp/cakephp/lib/Cake/bootstrap.php(175): Configure::bootstrap(true) #4 /app/webroot/index.php(101): include('/app/vendor/cak...') #5 {main} thrown in /app/vendor/cakephp/cakephp/lib/Cake/Cache/Cache.php on line 181

Scaling down to 1 dyno and the problem is gone. Any ideas on how to solve this?

UPDATE: I ran a debug(env('CACHE_URL')); just before the call to Cache::config I get the same result both when it works and when it fails. The connection string looks like this:

redis://redistogo:MySecretKey@greeneye.redistogo.com:10290//?prefix=APPNAME&duration=DURATION

I don't know how to debug Cake core when running in Heroku production environment.

Update 2: I've got the error in 2 different projects on 2 different Heroku deployments. I think @josegonzalez can reproduce it on http://pastebin.herokuapp.com/ since I've followed that tutorial.

Update 3: I added

debug($_SERVER['DYNO']); to see which dyno the app is running on. Running with 2 Dynos, web.1 has 100% succes rate. web.2 mostly fails, but sometimes manage to return the page. Running with 3 Dynos, web.1 still has 100% succes rate. web.2 fails more than before, web.3 has 100% fail rate.

AD7six commented 9 years ago

Please add to the ticket your cache config, i.e. what is $config when it fails?

josegonzalez commented 9 years ago

Interesting. I'll take a look at this later today, that doesnt sound like it should be happening...

thegallagher commented 9 years ago

I've also had problems using Redis to Go. I upgraded my plan to "Mini" and it fixed the problem. I also have &timeout=60 appended to my connection string. I can't remember if that was part of the fix or if I left that there accidentally while trying to fix my problems.

You're probably going to find you're having problems with your sessions next. I had to change the cache session handler.

josegonzalez commented 9 years ago

From https://devcenter.heroku.com/articles/redistogo

Why am I hitting my connection limit with one Dyno? This is often caused by customers using the nano free plan. One connection is used by one worker, but often other connections can be made to Redis. We would recommend upgrading to our lowest plan to see if this solves the issue. Otherwise we would recommend setting a hard limit on your application code.

HarderWork commented 9 years ago

Upgraded plan to "Mini" solved the 'CacheException' error! Thanks for pointing me in that direction. But instead I got random - but frequent - database connection problems on the web.2 dyno. I tried to set the session handler to 'cache' instead of 'php', but I guess thats what @thegallagher is talking about. Any tips and tricks on how to solve that part?

josegonzalez commented 9 years ago

Can you paste examples of the db connection errors?

thegallagher commented 9 years ago

@HarderWork I think I had this issue too. I ended up having to append &persistent=0 to my database connection string. This made my fixed my problem but I don't think it's the right way to do it.

Setting your session handler to cache allows your sessions to be shared between the 2 web dynos. This is a separate issue which I ran into when using multiple web dynos.

I probably should have reported all of these issues but I'm usually quiet until I can provide instructions to reproduce or fix a problem.

HarderWork commented 9 years ago

@josegonzalez It's a basic app-template project, so I'm just running on pages/home. And on web.2 it gets a lot of "Cake is NOT able to connect to the database." I didn't dig deeper at the moment. @thegallagher I'm to incompetent to be quiet :)

thegallagher commented 9 years ago

@HarderWork Now I know I'm not the only one with these problems I'll be more motivated. I'm curious to know if &persistent=0 fixed your problem?

HarderWork commented 9 years ago

Now it works!

Summary

CacheException: Solved by upgrading Redis To Go from Nano to Mini ($9/month at the moment). Changing duration and timeout didn't have any effect. Cake is NOT able to connect to the database: Solved by adding &persistent=0 to the DATABASE_URL

The only thing I changed in the app-template was to set 'cache' instead of 'php' as the default session handler in core.php. Don't know if it's relevant for this problem though.

Thanks for the help!