Question

Initial load is takes a lot of time in production compared to developement

I had the loading issue before and I’ve made a lot of improvements, i optimized the query issues, and now the only essential ones are made, i also put only the basic elements to load (i’m requesting the other elements that connect to DB later using ajax) to improve the initial load, the loading was indeed improved compared to before (from 15s to 6s-10s) in production and (from 15s to 2s-3s) in development, but as it figures the production still too slow, I’ve implemented all the improvement that I can on my part, i even upgraded to 4GB ram server but the same problem, i don’t have too much load on the server now 'cause i just started, Any idea what’s going on


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

alexdo
Site Moderator
Site Moderator badge
August 31, 2023

You could also examine your server’s resource utilization - CPU, IO, network - during these slow periods. High resource usage could indicate a bottleneck. If your individual services (like your DB, web server, etc.) are all on the same Droplet, splitting them up across multiple Droplets could help.

You may want to consider using monitoring tools or services to get more insight into what your application and server are doing during these slow times. DigitalOcean provides a monitoring service that can help with this.

https://www.digitalocean.com/community/tutorials/how-to-monitor-cpu-use-on-digitalocean-droplets

https://www.digitalocean.com/community/tutorials/how-to-use-top-netstat-du-other-tools-to-monitor-server-resources

Hope that this helps!

KFSys
Site Moderator
Site Moderator badge
August 29, 2023

Heya,

While you’ve taken a number of good steps to optimize, loading times of 6-10 seconds in production are still quite high. Here are some suggestions and areas you can investigate further:

  1. Database:

    • Ensure that your database is indexed properly. Use the EXPLAIN command before your SQL queries to see if they’re optimized.
    • Consider using a database profiler to detect slow queries.
    • Ensure that your database server isn’t the bottleneck. If your database is on a different server, network latency can add to the response time.
  2. Static and Media Files:

    • Use CDNs (Content Delivery Networks) like AWS S3 combined with CloudFront, or services like Cloudflare, to serve static and media files faster to users worldwide.
    • Ensure that you’re using Django’s ManifestStaticFilesStorage to add a version to your static files and avoid cache issues.
    • Optimize images and other media files to reduce their size.
  3. Web Server and WSGI:

    • Ensure that your web server (e.g., Nginx or Apache) is properly configured for performance.
    • Ensure that your WSGI server (e.g., Gunicorn or uWSGI) is optimally configured. If you’re using Gunicorn, for example, adjust the number of worker processes.
  4. Middleware:

    • Review the middlewares you are using in Django. Some middlewares can slow down the request/response cycle.
  5. Caching:

    • Use Django’s built-in caching mechanisms. Cache whole views if possible.
    • Use Redis or Memcached to cache data that is expensive to compute or fetch.
  6. Third-Party Services:

    • If you’re using third-party APIs or services, they might be slowing down your application. Ensure that these requests are optimized, or consider moving them to background tasks if they don’t need to be executed during the request/response cycle.
  7. Network:

    • Check the latency between your application server and any other external services (like databases, caches, third-party APIs). High latency can significantly impact performance.
  8. Profiling:

    • Use tools like Django Debug Toolbar in your development environment to identify bottlenecks.
    • Use logging in production to catch unexpected behavior or delays.
  9. Server Monitoring:

    • Tools like New Relic, Datadog, or even basic tools like htop or vmstat can give you insights into what’s consuming resources on your server.
  10. Content Delivery:

  • Minify and compress your CSS, JS, and HTML files.
  • Enable Gzip or Brotli compression on your web server.
  1. Background Tasks:
  • For tasks that don’t need to be executed immediately as part of the request/response cycle, use a background task manager like Celery.
  1. Django Settings:
  • Ensure that the DEBUG mode is set to False in production.
  • Ensure that the CONN_MAX_AGE parameter in your database settings is set to a reasonable value to reuse database connections.

If you’ve tried all of the above and you’re still facing issues, it might be worth it to bring in a Django expert to review your setup

Bobby Iliev
Site Moderator
Site Moderator badge
August 27, 2023

Hi there,

The difference in loading times between development and production environments, especially given that they both connect to the same database, could be caused by several things, but you need to keep in mind that if your production server is located geographically far from your database server (or your users), it could add significant latency. Where as when you connect to the app that is actually running locally on your laptop, there will be no network latency at all.

One thing that you could consider using a CDN for serving static assets to ensure they’re delivered quickly to users regardless of their geographic location.

Besides the network latency there are a few other things that you could check:

  1. Server Configuration:

    • Web Server: Are you using the same web server in both environments? Production setups often use a server like Nginx or Apache in front of the Django development server.
    • Static and Media Files: Ensure you’re serving these using Nginx or a similar web server and not through Django in production. Also, make sure they’re compressed and optimized.
    • Django Settings: Ensure DEBUG is set to False in production. Also, use the CONN_MAX_AGE parameter in your database settings to maintain persistent connections to the database.
  2. Middleware and Apps:

    • Check if you have any middleware or apps that run only in the production environment that could be causing delays.
  3. Database:

    • Even though you mentioned that both environments connect to the same database, ensure the database connection settings (pooling, connection limits, etc.) are optimized for production.
    • Use Django’s built-in database query logging to see if any unexpected queries are being run in production.
  4. Third-party Services:

    • Check if your production environment is making any external API calls or third-party service calls that are not being made in the development environment.
  5. Caching:

    • Implement caching for parts of your application that don’t change often. Django offers various caching mechanisms, including per-view caching, template fragment caching, and low-level caching.
  6. Server Monitoring:

    • Monitor CPU, memory, disk I/O, and network I/O on your production server. If any of these metrics spike during page loads, it could give you clues as to where the issue lies.
    • Consider using application performance monitoring tools like New Relic, Datadog, etc., to get insights into application-level performance metrics.
  7. TLS/SSL Overhead:

    • If your production environment is served over HTTPS (as it should be), and your development environment isn’t, consider the TLS handshake and the time it adds, especially if assets are loaded from multiple domains or subdomains.
  8. Optimize Assets:

    • Ensure that JS and CSS assets are minified and compressed for the production environment. Using tools like Webpack or Django Compressor can help with this.

If, after trying the above suggestions, you’re still facing issues, consider gradually replicating your production environment (software versions, configurations, etc.) in a local or staging setup. This can help isolate the problem by identifying at which step the performance degradation occurs.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel