laravel / octane

Supercharge your Laravel application's performance.
https://laravel.com/docs/octane
MIT License
3.75k stars 291 forks source link

Cache/Database Not Persisting Between Workers #377

Closed corsair closed 2 years ago

corsair commented 3 years ago

Description:

Hello there, I am also experiencing this issue while just using Octane under normal conditions.

When making changes to models, the cache (or otherwise database?) is not persisting to all workers. An example of this is when deleting a model and then refreshing a page that displays all remaining models, every refresh will result in either the just deleted model still persisting, or it being already deleted. This has made it virtually pointless to utilize/implement Octane because of this issue.

This issue does not occur when just using php artisan serve and only occurs with php artisan octane:start.

A video example: https://user-images.githubusercontent.com/1847891/133213342-2b9b0edc-a0f4-46da-8f6e-c24efe552ef0.mov

Let me know if anything else can be provided. I would like to utilize Octane for this application but have been unable to since this fundamentally makes it un-usable.

themsaid commented 3 years ago

Is it a cache issue or a database issue?

dorinniscu commented 3 years ago

Is it a cache issue or a database issue?

In my case it is a cache issue. I described in detail here: https://github.com/laravel/octane/issues/348

Namoshek commented 3 years ago

Are we talking about workers of the same Octane instance or of multiple servers/containers? The Octane cache works per instance, if Swoole is used.

corsair commented 3 years ago

@Namoshek For me, this is just a single server and running it locally on my laptop (Macbook Pro) using PHP and Mysql installed via Brew. The same issue exists on my other development setup, on Windows 10.

This appears to be a database issue in my circumstance since the code workflow is rather simple.

I call Auth::user()->characters (one-to-many relationship) which is then passed to the frontend (Blade), so this should solely be database-related. I assumed this was the same issue as @dorinniscu, since I was getting the same error upon booting up Octane.

-- Edit: If it matters, here are the dependencies I'm currently using on this project.

"require": {
        "php": "^8.0",
        "ext-json": "*",
        "consoletvs/profanity": "^3.0",
        "fideloper/proxy": "^4.4",
        "fruitcake/laravel-cors": "^2.0",
        "graham-campbell/markdown": "^13.0",
        "guzzlehttp/guzzle": "^7.0.1",
        "intervention/image": "^2.5.1",
        "laravel/framework": "^8.12",
        "laravel/octane": "^1.0",
        "laravel/telescope": "^4.4",
        "laravel/tinker": "^2.5",
        "laravel/ui": "^3.2",
        "pragmarx/google2fa-laravel": "^1.0"
    },
    "require-dev": {
        "roave/security-advisories": "dev-master",
        "facade/ignition": "^2.5",
        "fakerphp/faker": "^1.9.1",
        "laravel/breeze": "^1.0",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.2",
        "nunomaduro/collision": "^5.0",
        "phpunit/phpunit": "^9.3.3"
    },
sy-records commented 3 years ago

In my case it is a cache issue. I described in detail here: #348

Actually I didn't reproduce your problem, can you provide a repository for reproducing the code?

themsaid commented 3 years ago

Use Auth::user()->characters()->get() instead and try again.

Elycin commented 3 years ago

I think there's a little more information than meets the eye here.

Not knowing the codebase in question, there is probably a polymorphic relationship for the characters property on the model via oneToMany. This makes use of eager loading, calling the method as you described would make an additional database call which would be detrimental to efficiency.

eg:

use App\Models\Characters;

class User extends Model {
    public function characters() {
         return $this->hasMany(Characters::class, 'user_id', 'id');
    }
}

@themsaid

corsair commented 3 years ago

@themsaid @driesvints

Switching to Auth::user()->characters()->get() seems to have resolved the issue; so what does this say regarding Octane/this issue? I read on some blogs that some behaviors have to change when working with Octane/Swoole, but the notion of having to change entire codebases feels flawed by design. The fact that this works perfectly fine without Octane (when using php artisan serve or via Nginx) but breaks when utilizing Octane feels problematic.

Nothing in the Laravel documentation (as far as I've read/seen) mentions having to adjust relationship calls in this manner, so if this is a requirement to avoid application-breaking issues, it should be documented.

To match what @Elycin shared, the relationship is set up as a oneToMany.

public function characters()
    {
        return $this->hasMany(Character::class);
    }
corsair commented 3 years ago

As an update, it appears to be an issue only impacting Octane + Swoole. Switching to Octane + Roadrunner further resolves this database/cache persisting issue.

No other code changes nor configurations were altered between Swoole and Roadrunner.

dorinniscu commented 2 years ago

As an update, it appears to be an issue only impacting Octane + Swoole. Switching to Octane + Roadrunner further resolves this database/cache persisting issue.

No other code changes nor configurations were altered between Swoole and Roadrunner.

I just tested it and Octane (Swoole) cache store works properly. I reported the issue somewhere in July and never tested after that. It was only a test to see how Laravel Octane works.

Laravel Framework v8.61.0 / Laravel Octane v1.0.11

cache()->store('octane')->put("test_key", 'test_value', now()->addMinutes());

cache()->store('octane')->get("test_key"); => returns 'test_value'

Maybe was something related to Swoole table driver (just guessing) but seems to be fine now.

themsaid commented 2 years ago

@corsair I cannot replicate your issue. The user instance changes after every request and all cached relationships are flushed.

If you can replicate this in a fresh Laravel app and share it on GitHub it'll help me debug the issue. Otherwise, there seem to be a problem with your specific app.

corsair commented 2 years ago

@themsaid Sounds good and will close this issue for the time being until I've successfully replicated this on a fresh install or otherwise found another root cause (and will report back here if so).