Magento 2 - Best Session Storage

๐Ÿ—„๏ธ
MySQL?
OR
โšก
Redis?
OR
๐Ÿ“
Files?

Introduction

When developing Magento 2 e-commerce stores, choosing the right session storage mechanism is critically important. It affects not only site performance but also user data integrity. In this article, we'll compare three main types of storage.

How Sessions Work in Magento 2

Before testing, it's important to understand the key principles of session operation:

  • All operations happen in PHP memory โ€” set and get methods don't write data immediately to storage
  • Data loading occurs when calling session_start() at the beginning of application execution
  • Data saving happens only when the script terminates via writeClose()
โš ๏ธ Important: If two requests work in parallel with the same session, each starts with identical state and can overwrite the other's changes.

Session Types in Magento 2

Magento 2 has several typical session storage configurations:

  • DB โ€” storage in MySQL database
  • Files โ€” storage in filesystem
  • Redis disable_locking => 1 โ€” without locking (fast but risky)
  • Redis disable_locking => 0 โ€” with full locking (safe but slow)
  • Redis disable_locking => 0, break_after_frontend => N โ€” with limited locking (compromise)

Testing Methodology

For objective evaluation, we created a special test script that simulates a real situation โ€” two concurrent requests trying to write different data to the same session.

Test Scenario:

  1. Session cleanup โ€” start with clean state
  2. Parallel execution of two requests in different browser tabs:
    • Request A: runs for 30 seconds and writes value=X
    • Request B: completes quickly and writes value=Y
  3. Result verification โ€” which data remained in the session?

This test reveals two critical characteristics:

  • Data integrity โ€” were both values preserved or did one overwrite the other?
  • Performance โ€” how long did the second request wait for the first to complete?

Test Results

All results are consolidated in a comparison table:

Storage Configuration Locking Performance Test Result Data Lost? Recommendation
DB Default โŒ No โšกโšกโšก Instant X
Y lost
โœ… YES โ›” Not recommended
Files Default โœ… Yes โš ๏ธ Waits 15 sec XY
Nothing lost
โŒ NO โš ๏ธ Dev only
Redis disable_locking => 0 โœ… Yes โš ๏ธ Waits 15 sec XY
Nothing lost
โŒ NO โš ๏ธ For critical data only
Redis disable_locking => 0
break_after_frontend => 60
โš ๏ธ Partial (60 sec) โš ๏ธ Waits up to 60 sec XY
Nothing lost
โŒ NO โš ๏ธ Too slow

Final Observations

While it's clear that disable_locking => 1 can lead to data overwrites during parallel requests, in practice this happens infrequently. It typically occurs with parallel AJAX requests or when users work very intensively across multiple tabs on your site.

โ„น๏ธ Magento Cloud: In Magento Cloud, disable_locking => 1 is the default value. This is because with disable_locking => 0 you get perfect session integrity, but locks occur very frequently, especially with poorly written code.

For example, one resource-intensive operation can completely block site access. You may see this during real-time admin operations like exports. If an export takes 1 minute, you've probably noticed you can't navigate the admin panel. This is session lock. Therefore, a compromise is needed.

Our Recommendations

1. Use disable_locking => 1

Eliminate parallel AJAX requests during checkout as much as possible, especially during place order. This will prevent most data loss issues.

2. Alternative: disable_locking => 0, break_after_frontend => 5

This configuration provides balance between performance and data safety. A 5-second lock is sufficient for most quick operations (add to cart, update quantity), but doesn't completely block the site during long operations.

Why this works:

  • Most AJAX requests in Magento 2 complete within 1-3 seconds
  • If a request completes faster than 5 seconds โ€” data is saved correctly
  • Long operations (over 5 seconds) don't block users in other tabs
  • Reduces risk of site "freezing" with code issues
โš ๏ธ Warning: When using break_after_frontend => 5, ensure critical operations (place order, payment) complete faster than this time. Otherwise, use a higher value (15-30 seconds).

3. disable_locking => 0 โ€” only in extreme cases

We recommend using full locking only if you have:

  • Frequent data loss in orders
  • Strange cart or checkout behavior
  • Critical financial operations requiring 100% guarantee

Even then, we advise detailed debugging first and returning to disable_locking => 1 after fixing the root cause.

๐Ÿ†˜ Need debugging help?

Contact us for assistance โ€” we have extensive experience in this area. We'll help identify and fix the problem without needing to slow down your entire site.

Contact Us โ†’

4. Files/DB sessions โ€” not recommended

We don't recommend using file-based or database sessions โ€” use Redis instead. Only if you don't have Redis available, choose one of these options.

๐Ÿšจ Critical for clustered solutions: If you have multiple nodes or autoscaling, file sessions may work unstably since files aren't synchronized between servers.

Summary Recommendations Table

Use Case Recommended Configuration
High-traffic e-commerce store disable_locking => 1 + AJAX optimization
Medium-traffic store disable_locking => 0, break_after_frontend => 15
Store with critical data (payments, B2B) disable_locking => 0, break_after_frontend => 30-60
Development and testing Files or disable_locking => 0
Content site without cart disable_locking => 1

Conclusion

Choosing the right session storage mechanism is critically important for stable Magento 2 operation. There's no universal solution โ€” the choice depends on load, data type, and your project's business requirements.

Our experience shows that Redis with disable_locking => 1 or break_after_frontend => 15 provides optimal balance between performance and reliability for most e-commerce stores.