0

I'm a little worried about Laravel's performance with database usage. I have noticed that with every refresh or connection to the server, Laravel Auth makes a connection to the database to get the user by the 'ID'. Why not save the user in the session? Is there a way to change that? Models also retrieve a lot of information and some unnecessary ones.

I have the feeling that this makes the application slow and that I have no control over these connections... :/

Some tips?

Clark
  • 15
  • 4
  • Slow? Not really... Session stores logged in user id so you can retrieve user model from the database... many reasons for this which outweigh the small query ... Like getting up to date info from the db... remember that users can login from multiple devices... come update their info... and access with another device with outdated user info if it is all in the session... What are you doing that require such performance? – Serge Jul 24 '18 at 02:02
  • I've been working with Zend for a long time and you can store the user auth object in the session. That's why I was a little worried about getting the user every request from the database with Laravel. But I think that what you said makes sense... Thanks – Clark Jul 24 '18 at 12:21

1 Answers1

0

Short answer: Don't be worried. For most applications there won't be an issue.

Long answer: If you look into the framework level code a bit and see how laravel actually handles the request when you call Auth::User() you will see that for the authenticated user every request goes through SessionGuard which immediately returns the user instead of making a database call.

https://github.com/laravel/framework/blob/5.6/src/Illuminate/Auth/SessionGuard.php#L122

public function user()
{
    if ($this->loggedOut) {
        return;
    }
    // If we've already retrieved the user for the current request we can just
    // return it back immediately. We do not want to fetch the user data on
    // every call to this method because that would be tremendously slow.
    if (! is_null($this->user)) {
        return $this->user;
    }

So your first hit is going to make a trip to database and the subsequent request should just return from session as shown below. You can validate this by enabling Database Query logging in Laravel and see what queries are making trip to the database. For further info on Query Logging you can read the doc per laravel veersion or this conversation.

From a performance standpoint, unless you are going to have thousands of concurrent users hitting the site on a DB heavy application then DB connection is going to be the least of your problem, You will run into other issues first before the database gets bogged down. I don't have much of a context so I'm making assumptions. You can always throw more commodity hardware or move caching to another datastore such as MemCache or Redis if you start to notice performance degrading. The reason you've chosen Laravel (PHP) is for speed in development and not necessarily for performance.

But it's great that you are keeping performance in mind and you're thinking through these things as you build your application.

na-98
  • 909
  • 8
  • 16