cancel
Showing results for 
Search instead for 
Did you mean: 

Best Practices for Optimizing Magento 2 Code

Best Practices for Optimizing Magento 2 Code

Hi everyone,

I’ve been working on a Magento 2 project and want to make sure I’m following the right practices for clean and optimized code. I’m especially curious about: Reducing unnecessary queries in custom modules.

Best ways to handle caching for dynamic content.

Tips for keeping extensions lightweight and upgrade-friendly

What strategies or common pitfalls should I keep in mind? Any recommendations from your own projects would be super helpful.

Thanks in advance for your insights

1 REPLY 1

Re: Best Practices for Optimizing Magento 2 Code

Hi @hananbukha368e ,

  1. No ObjectManager calls in business logic.
  2. Public API exposed via interfaces (service contracts).
  3. No models loaded inside loops batch loading used.
  4. Collections select only needed fields.
  5. Heavy operations moved to cron/async or indexers.
  6. Use cache tags and Redis for transient/calc data.
  7. sections.xml/customer-data used for per-customer dynamic bits.
  8. Declarative schema & data patches used.
  9. Unit/integration tests added for business logic.

 

More information please refer below

  1. https://developer.adobe.com/commerce/php/best-practices/
  2. https://www.scommerce-mage.com/blog/magento-2-full-page-caching-hole-punching-using-section.html
  3. https://devdocs.mage-os.org/docs/main/performance-best-practices

 

Problem Solved? Accept as Solution!

 

Thanks,

Ankit

Ankit Jasani