Laravel – extremely high TTFB (~11 seconds!)

  laravel, laravel-5, php

I hate to present an issue without a means of repro, but I’m having a real difficult time with an obscenely high TTFB on a Laravel instance I’m running. Please bear with me and offer an area I can look at if you’re aware of something I’m not? 😬

Locally in my app, I have a TTFB of ~1.4s to return the query:
enter image description here

When it’s on my remote server, the exact same query has a TTFB of ~11 seconds. I understand the content download would be slower, but I’m not clear why there’d be such a gargantuan drop in TTFB.

enter image description here

Again, please don’t flame me for not submitting a reproducible example – I’m merely after some avenues to investigate, as I’m having a really hard time getting to the bottom of it.

If it’s helpful:

  • it only seems to affect this query
  • Laravel itself seems to run it very quickly (wall-clock time)
  • there’s no fancy middleware or anything, but it’d presumably be a fair bit of JSON (860kb).
  • database is hosted on the same EC2 instance as Laravel itself, so not latency issues there

If it’s not related to download speed itself, can anyone think of where else the issue may lie?

    public function getUserData(Request $request){
        if(!isset($_GET['id'])){
            return response('No user ID', 400);
        }

        $id = $_GET['id'];

        $user = User::find($id); // possible eager load?

        if($user){
            $user->touch();

            $user->last_log_in = Carbon::now();
            $user->save();

            $user->load(['accounts.users', 'emailPreference', 'sessions', 'minTodoDays', 'minNotifications']);

            $user->todo_days = $user->minTodoDays;
            $user->notifications = $user->minNotifications;

            unset($user->minTodoDays);
            unset($user->minNotifications);

            $user->chats = $user->chats()->orderBy('updated_at','desc')->with('minChatItems')->take(30)->get();

            foreach($user->chats as $key=>$chat){
                $user->chats[$key]->chat_items = $chat->minChatItems;

                unset($user->chats[$key]->minChatItems);
            }

            if(!$user->country){
                Queue::push(new SubscribeUser($user, $request->ip()));
            }

            return $user;
        }else{
            return response('No user found', 404);
        }
    }

Source: Laravel

Leave a Reply