Recommended Database Configuration taking online quiz with Moodle Hosted on Google Cloud

Recommended Database Configuration taking online quiz with Moodle Hosted on Google Cloud

by Arnold Babasa -
Number of replies: 9

Hi,

I am fairly certain this topic has been discussed over and over again. Forgive me for sounding repetitive. I have been, you could say, thrown into the deep end in getting our University LMS to accommodate online exams. We are currently hosting on Google Cloud and this is pretty much our architecture (they are all in the same region):

- 1 Load Balancer to distribute traffic to Compute Engine
- Compute Engine launches the VMs hosting Moodle code (16GB vcpu and 64GB RAM) scaled between 1 and 3 instances
- Filestore (1 TB) for Moodledata automounted on the auto-scaled VMs
- Cloud SQL for our PostgreSQL Instance (8GB vcpu and 32GB RAM)

At first, the platform was used to allow students to access assignments which they would download and submit at a later time. This worked out well. Problems began when we introduced real time quizes. We started with 500 people which the system managed well. When we scaled out to 2000 users, the numbers of requests overwhelmed the database which eventually crashed. We were introduced to the dreaded error:

db=moodledb,user=postgres FATAL:  remaining connection slots are reserved for non-replication superuser connections"

After reading on a number of forums, I decided to place a pgBouncer instance between the VMs and PostreSQL. I did not perform any customizations other than adding the database connection details. Now, instead of the message above, I get multiple messages like this:

ERROR accept() failed: Too many open files
closing because: client close request (age=160s)
closing because: client close request (age=90s)
WARNING C-0x55c61cc57780: (nodb)/(nouser)...pooler error: no more connections allowed (max_client_conn)
closing because: no more connections allowed (max_client_conn) (age=0s)

I am stuck and we have been off all day. I doubt we can afford to go be off another day. I will be grateful for any advice on what I can do. If I need to provision more resources. More RAM or something else. If there are any configurations I can look out for? For a bit more context, I happen to have the exam timetable and the largest real-time quiz has 2300 people. Although this does not account for other students who will be browsing at that time. Thanks everyone. Looking forward to your responses.

Average of ratings: -
In reply to Arnold Babasa

Re: Recommended Database Configuration taking online quiz with Moodle Hosted on Google Cloud

by Alex Rowe -
I don't use PostreSQL so there isn't much to offer there but the application servers should be enough.

Have the application servers been properly configured to use the maximum amount of RAM? e.g. Apache Event MPM or NGINX with PHP FPM and updating the Apache/NGINX/PHP workers to use all your RAM?

What is your Moodle version?

You don't mention a MUC cache, or other caching? Do you have application cache and PHP session cache in Redis or something else?

First result on Google for "remaining connection slots are reserved for non-replication superuser connections" is https://support.microfocus.com/kb/doc.php?id=7024137. It mentions to update your max_connections as they have run out. Have you done that?

First result in Google for "ERROR accept() failed: Too many open files" is https://stackoverflow.com/questions/42429112/pgbouncer-error-accept-failed-too-many-open-files. You have to increase the system open files limit for pgbouncer. Have you done that?

Do you have any other monitoring to show where the bottlenecks are?
Average of ratings: Useful (1)
In reply to Alex Rowe

Re: Recommended Database Configuration taking online quiz with Moodle Hosted on Google Cloud

by Arnold Babasa -

Hi Alex,

Thank you for your detailed response. I will be happy to answer and also expand my findings.

1. Each instance is running Nginx with PHP-FPM (PHP7.4). Each instance has the following (dynamic) configuration options set for the fpm pool:

pm.max_children = 880
pm.start_servers = 32
pm.min_spare_servers = 16
pm.max_spare_servers = 32

2. I am running Moodle 3.11.3+

3. Yes, we are using Redis as the Application and Session Caches. We also have a memcached instance but for some reason we get an error saying "this store does not requirements for all store definitions". So we stuck to redis and it's performing well.

4. I managed to fix the Database Connection error by "properly" configuring PostgreSQL and pgBouncer. There are a couple of settings that I had to tweak on PostgreSQL itself, most notably shared_buffers & effective_cache_size. pgBouncer also required specific configurations especially max_client_conn and default_pool_size. After playing with different settings I managed to get something I could work with.

5. Yes, I do have a good amount of logging done with the Google Cloud console. I am able to see everything that's going on. Resources used and so on. Helps me to plan ahead.

You raised something that I would love if you explained it more. You said, "updating the Apache/NGINX/PHP workers to use all your RAM?". Is there a benefit to this? Would this improve performance? Also, I seem to see a lot of lines in my fpm-slowlog.log and it looks like 95% are traced from the index.php like:

script_filename = /var/www/html/moodle/index.php
[0x00007f4a54e160d0] fopen() /var/www/html/moodle/cache/stores/file/lib.php:358
[0x00007f4a54e15fe0] get() /var/www/html/moodle/cache/classes/loaders.php:414
[0x00007f4a54e15f10] get() /var/www/html/moodle/cache/classes/loaders.php:1572
[0x00007f4a54e15e80] get() /var/www/html/moodle/lib/weblib.php:1810
[0x00007f4a54e15cc0] purify_html() /var/www/html/moodle/lib/weblib.php:1709
[0x00007f4a54e15c20] clean_text() /var/www/html/moodle/lib/weblib.php:1301
[0x00007f4a54e15ae0] format_text() /var/www/html/moodle/lib/externallib.php:1036
[0x00007f4a54e159b0] external_format_text() /var/www/html/moodle/lib/classes/external/exporter.php:161
[0x00007f4a54e15840] export() /var/www/html/moodle/calendar/classes/external/day_exporter.php:213
[0x00007f4a54e157c0] core_calendar\external\{closure}() /var/www/html/moodle/calendar/classes/external/day_exporter.php:212
[0x00007f4a54e15750] array_map() /var/www/html/moodle/calendar/classes/external/day_exporter.php:212
[0x00007f4a54e15630] get_other_values() /var/www/html/moodle/calendar/classes/external/week_day_exporter.php:104
[0x00007f4a54e15590] get_other_values() /var/www/html/moodle/lib/classes/external/exporter.php:125
[0x00007f4a54e15420] export() /var/www/html/moodle/calendar/classes/external/week_exporter.php:164
[0x00007f4a54e15260] get_other_values() /var/www/html/moodle/lib/classes/external/exporter.php:125
[0x00007f4a54e150f0] export() /var/www/html/moodle/calendar/classes/external/month_exporter.php:312
[0x00007f4a54e14fa0] get_weeks() /var/www/html/moodle/calendar/classes/external/month_exporter.php:215
[0x00007f4a54e14ea0] get_other_values() /var/www/html/moodle/lib/classes/external/exporter.php:125
[0x00007f4a54e14d30] export() /var/www/html/moodle/calendar/lib.php:3499
[0x00007f4a54e14b00] calendar_get_view() /var/www/html/moodle/blocks/calendar_month/block_calendar_month.php:55

Is there something I can improve to make this faster? Could it be because the Moodledata files are hosted on a Network (shared) file system? Thank you for your response. 

In reply to Arnold Babasa

Re: Recommended Database Configuration taking online quiz with Moodle Hosted on Google Cloud

by Arnold Babasa -
I got the culprit. I had misconfigured a plugin "Courses to Calendar" that caused my home page to delay loading. I am still investigating why. But after disabling it, m home page now loads in < 2s.
In reply to Arnold Babasa

Re: Recommended Database Configuration taking online quiz with Moodle Hosted on Google Cloud

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
What version of Moodle and Courses to Calendar are you using? This should not be causing a delay like that...
In reply to Emma Richardson

Re: Recommended Database Configuration taking online quiz with Moodle Hosted on Google Cloud

by Arnold Babasa -
We are using Moodle 3.11.3+ and the Courses to Calendar is 3.9.2.
I am extracting more information from the logs that I can share.
In reply to Arnold Babasa

Re: Recommended Database Configuration taking online quiz with Moodle Hosted on Google Cloud

by Alex Rowe -
My point around Apache/NGINX/PHP workers was to make sure you weren't on the default out of the box values and that you have already set them up to make use of your (64GB) RAM.

It looks like you have already set those up, but the value of PHP max children looks to be quite large. Do you know how large your PHP processes are on average? If so, times that by 880 and it would be the max RAM PHP can use and that would be over what the server is capable of.

As long as only your Moodle data directory is on a shared file system and your MUC/Sessions are on Redis and you have the local cache configured as well, the performance should be good.

There are also some performance issues in having calendar type blocks on the home page when you have a lot of events in the database but there are trackers open for that at the moment. The performance issues get exacerbated with quizzes as you have a lot of users all logging in at the same time to get ready for the quiz, so not only is there users for the quiz, but logins, home page loads etc all impact the DB and performance.
In reply to Alex Rowe

Re: Recommended Database Configuration taking online quiz with Moodle Hosted on Google Cloud

by Arnold Babasa -
Hi Alex, you're right. After calculating again, the Max workers are about 750. The PHP-FPM processes take on average 82MB of RAM and the free memory is 62GB.

The performance is quite good now. Very fast page loads even with the Standard Persistent Storage and not the SSD.

Yes, there are some issues and I am following various trackers. I have disabled the calendar block for now, at least until exams are done. Today we attempted a quiz of 1500 users for one hour and it went very well. I forgot to mention that we are also using the Delayed Quiz plugin to spread out the start times for students to access the quiz. This has helped to reduce the impact of initial login.

However, we have a quiz of 2000 coming up soon (largest class) and we are wondering whether to increase database from 64GB RAM to 96GB to improve the work_mem, shared_buffers and effective_cache_size of our PostgreSQL instance.

Cheers.


In reply to Arnold Babasa

Re: Recommended Database Configuration taking online quiz with Moodle Hosted on Google Cloud

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
By "real-time quiz" do you mean this plugin
https://moodle.org/plugins/mod_realtimequiz
In reply to Marcus Green

Re: Recommended Database Configuration taking online quiz with Moodle Hosted on Google Cloud

by Arnold Babasa -
Hi Marcus, sorry I did not specify. We are actually using the Safe Exam Browser for our online exams.