We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45778
    • 75 Posts
    In my tests, anonymous page views take several times longer to load than the same pages when logged in. This is a pattern across my entire MODX site, no matter which template I use, and no matter how basic or complicated the template is, or how much content is on the page. The same exact page/template loads much more slowly if I'm not logged in.

    I can't figure out why.

    I'm purposely testing a page that is completely uncached (the page is set to cacheable=0) as my baseline, because that should show the basic raw speed of the system. Shouldn't an anonymous page load more quickly? Or at least shouldn't it be the same speed as the authenticated pages? What is it about authentication that makes it faster? Or about anonymous sessions that make them slower?

    I don't know all of the variables that matter, but here are some of my system settings:
    anonymous_sessions: Yes
    cache_db_session: Yes
    cache_db: Yes

    This question has been answered by paulbohman. See the first response.

      • 45778
      • 75 Posts
      I'll add that my database tables are MyISAM (everything got much slower after switching to InnoDB, so I switched back. See https://forums.modx.com/thread/103233/potential-drawbacks-with-innodb-clustered-indexes-and-modx-session-structure for a related thread about InnoDB).

      And one more setting that may be relevant:
      session_handler_class: modSessionHandler
        • 45778
        • 75 Posts
        After a bit of experimentation, I've found that disabling the sessions for anonymous users makes a HUGE difference:

        anonymous_sessions: No

        The site is a lot faster like this, essentially as fast as the authenticated sessions. But I still don't understand why the anonymous sessions were slower in the first place. If both the authenticated and the anonymous sessions are being tracked in the database, what is so special about anonymous sessions that makes them take longer?

        I don't think I need anonymous sessions to be tracked, but I'm not 100% sure that's true. I may find a need for tracking later, or maybe I'm already using it somewhere on the site that I've forgotten about, so I'm a bit nervous that I may be breaking something, either now or in the future.

        It still would be nice to know how to make anonymous sessions as fast as authenticated sessions.
          • 45778
          • 75 Posts
          And I spoke too soon. Turning off anonymous sessions broke my authenticated sessions, though I'm not sure why.
            • 45778
            • 75 Posts
            Here is some more information about the behavior I'm seeing. I created a template with only hard-coded content in it:

            <head>
                <title>Hard coded page title</title>
            </head>
            <body>
            <h1>Hard coded heading</h1>
            <p>Hard coded content</p>
            </body>
            </html>


            And I created a page with no content in it at all. It has a page title and an alias and nothing else. I made this page NOT cacheable, to test the raw processing speed. The page is NOT a member of any resource groups. There are NO template variables associated with this template.

            Here's what I found:

            On average, the network request/response time is as follows:


            • Not logged in: 54ms
            • Logged in: 28ms

            If I add the page to a resource group that is available to the anonymous user group, the resulting speed is as follows, on average:


            • Not logged in: 76ms
            • Logged in: 28ms (no change)

            If I add template variables, the speed decreases overall, whether logged in or not, but much more when not logged in. On a complex page with a lot of content and template variables and logic, the results are as follows:


            • Not logged in: 1800ms (nearly 2 seconds)
            • Logged in: 176ms

            Conclusion:

            So no matter what I do, unauthenticated page load time is always significantly more than the authenticated page load time (in the absolute simplest use case, it is about double the load time), which seems backward from what it ought to be. I would think that it takes less work to NOT check permissions than to check them.

            And the more options I add to pages (resource groups, template variables, programming logic), the greater the gap. In my resource-intensive page, the anonymous page load time is slower by about a factor of 10.

            Any insights as to how to fix this?
            • discuss.answer
              • 45778
              • 75 Posts
              I've discovered why I was seeing these results, and I'm kind of embarrassed that I didn't realize the reason sooner.

              The authenticated user that I was testing with was a sudo user in MODX, which meant that MODX was ignoring the permissions and just giving me access to the pages. It's no wonder that the sudo user was faster than any other user, including faster than the anonymous user.

              When I tested with non-sudo users, the load time was similar to the anonymous user.

              Mystery solved.