Faster storefront reads without a new database


Series cover, part 4 of 4: the same Shopware code connected to MySQL 8.4, MariaDB 10.11, PostgreSQL 18 and SQLite, all highlighted.

Three parts in, the question has changed. Part 1 asked whether Shopware can run on four databases from the same code, part 2 looked at what MySQL forgives, and part 3 closed most of the performance gap. This last part is about the biggest speed win of the whole project, and it has nothing to do with which database you run.

Here is the problem in one sentence: for every storefront page, Shopware rebuilds the same products from dozens of tables, again and again. With 150,000 products, eight parallel users and search still running in SQL, the DAL kept eight MySQL cores busy and served 14 requests per second. A faster database does not fix that. Doing less work does.

Two questions came up on the way. Should Shopware use a “real” ORM instead of its own DAL? And if the products are rebuilt for every request, why not keep them ready-made? The answers were “no” and “yes, but carefully”.

Note: This is research on a proof-of-concept branch. Nothing here is a supported Shopware feature. The read store below is a prototype, measured on one machine and not filed upstream yet.

Why not just use an ORM?Link to this section

Every few months somebody asks me why Shopware 6 has no Doctrine ORM, like Shopware 5 had. There is no decision record for it. The only source I found is a slide from a 2019 talk on the Shopware 6 core architecture: difficult to optimize, not suitable for core concepts, entities not extendable by plugins, not possible to enforce patterns.

Seven years later, I wanted to know if that still holds. So I built the same five storefront and API scenarios with Doctrine ORM 3.8 and Cycle ORM 2.19: a listing, a variant detail page, a search with filters and aggregations, a bulk write of 50 products and a search from criteria JSON. Then I checked the things Shopware needs from its data layer.

Neither ORM knows Shopware’s core concepts. Translations with fallback, variant inheritance, versioning with merge and write events: neither ORM provides any of them. Every check that passed, passed with hand-written code next to the ORM. And Doctrine failed outright on the most important one for an ecosystem: a plugin cannot add a field to a core entity, because the core class would have to declare it.

Fast only when it stops being an ORM. Optimized Doctrine came close to raw SQL on reads, with the same number of statements, but only with scalar queries and array results. At that point you have left the entity model, which is the reason you would pick an ORM in the first place. On writes, both ORMs sent one statement per row, just like the DAL.

And it would break every plugin. Replacing the DAL changes the extension API of every plugin and app. I estimated the full switch at 100 to 230 person-weeks, two to four and a half years of one developer. An ORM hidden underneath the DAL would avoid the break, but costs 30 to 60 person-weeks for no benefit I could show.

So the 2019 reasons still hold. Keep the DAL.

DAL v2: the same API, less workLink to this section

Keeping the DAL does not mean leaving it as it is. DAL v2 is a set of changes behind the existing API, built next to the DAL without editing a DAL class, each one switchable on its own. Four of them carry most of the gain:

  • Count in the same query. A paginated listing asks for the total number of hits. Today that is a second query. A window function ( COUNT(*) OVER() ) delivers the total with the ids. Counting the total with a window function is open.
  • Read only what you need. Most pages need a few fields of a product, not the whole entity with every association. Typed projections ask for those fields through the normal repository, with no new SQL path. Typed projections through the repository is open.
  • Batch the updates. The write gateway sends one UPDATE per row. Grouping rows that change the same columns into one statement, together with the fix below, took the 50-product write from 371 statements to 27. Batching update commands is open.
  • Don’t look up what you already have. The hidden query per row from part 2. That fix is already merged.

On 5,000 products on MySQL, with the result checked against the old DAL every time:

ScenarioDALDAL v2Faster
Listing10.8 ms4.8 ms2.27x
Detail5.5 ms2.3 ms2.38x
Filters and aggregations58.8 ms34.4 ms1.71x
Search from criteria JSON29.9 ms14.0 ms2.14x

The same picture showed up on MariaDB and PostgreSQL. I estimated DAL v2 at 11 to 27 person-weeks without a public API break, against 100 to 230 for an ORM. That is the cheapest speed-up in this whole series, and it helps every shop on every database.

But at 150,000 products it is not enough. DAL v2 makes reads there 1.24x to 2.15x faster and lifts throughput by 24 %, from 14 to 17 requests per second, with search still in SQL. MySQL still runs eight cores flat out. Making each query cheaper does not help much when the problem is how many of them there are.

The read store: stop rebuilding productsLink to this section

Look at what a storefront listing does today. OpenSearch finds the matching product ids, fast. Then Shopware asks MySQL for the full products behind those ids: translations, prices, media, properties, variant inheritance, all assembled by the DAL. For every request, even though the products have not changed since the last one.

The idea of a read store is simple: build each product once, when it changes, and keep the finished document. Reads then fetch documents by id instead of rebuilding them.

The best place to keep them turned out to be the one Shopware already has. OpenSearch can store a field that it does not index, and it returns it with the search hits ( _source ). So the product index got one extra field, rs_document , mapped with “enabled”: false , holding the whole product: all languages, currencies and rule prices, about 4 KB per product in the test catalog. The search still runs on the existing OpenSearch code. Only the step after it changes: instead of asking MySQL, Shopware reads the document that came back with the hit.

At 150,000 products, eight parallel users, on MySQL:

MetricToday (ids, then DAL)Document from OpenSearch
Throughput532 req/s756 req/s
MySQL CPU in 15 s of load15.1 s2.44 s
Product detail7.64 ms3.37 ms

That is 1.42x the throughput with 84 % less MySQL CPU, on top of what OpenSearch search already gives today. The CPU number is the one that changed how I see the whole project. Once the products are built, MySQL barely takes part in a storefront read. Which database you pick matters much less than how often you ask it the same question.

Valkey for single products. An in-memory key-value store like Valkey is even faster for reads by id: a product detail in 0.16 ms instead of about 4 ms (on 5,000 products; I did not build the combined store at 150,000). But Valkey cannot search. The combined OpenSearch and Valkey store reached only 0.54x the throughput and put 22 times the CPU load on MySQL, because that prototype still ran Shopware’s SQL keyword lookup before it queried the store. So Valkey is an option for detail pages and batches of known ids, next to OpenSearch, not instead of it.

Building the documents. A projector listens to the events Shopware already fires after indexing and stock changes, and rebuilds the affected documents. Building them with plain SQL instead of the DAL was 4.68x faster. A plugin that wants its data in the document adds it with a small extender of about 30 lines.

What it costs. Nothing here is free:

  • The product index grows from 190 MB to 462 MB at 150,000 products, and a full bin/console es:index takes 170 s instead of 88 s.
  • Custom fields and languages make documents big. The median document is 3.9 KB with one language and no custom fields, 52 KB with 1,000 fields in 5 languages and 934 KB with 5,000 fields in 20 languages. Valkey does not help there, it stores the same JSON.
  • Stock in the document is a snapshot. During the short lag a page can show an old stock, and checkout still validates against the database.
  • Plugins that write to the database with raw SQL bypass the events, so their changes do not reach the documents until the next rebuild. Part 2 showed how many plugins write their own SQL.

What I tried and droppedLink to this section

Not every idea survived the numbers, and some of the most useful results of the project were a clear “no”.

JSON documents in the SQL database. Store the document as JSON in MySQL or PostgreSQL and query it there. Reading one document by id was very fast. But filters and aggregations over 150,000 JSON documents took 4.85 s on MySQL, slower than the normal DAL with 2.72 s. PostgreSQL spent its time unpacking compressed JSON. It stays an option for reads by id only.

MongoDB. It was fast. But OpenSearch was faster on the queries, so its license (SSPL) and one more system to run were not worth it.

One query layer for all engines. I also built a shared query layer that runs the same filters on SQL, MongoDB, Valkey and OpenSearch. It costs about twice the effort, 40 to 68 person-weeks against 22 to 37 for the OpenSearch document. And it lost: a word search took 574 ms through that layer and 25.5 ms through today’s OpenSearch query.

The cache trapLink to this section

This is the finding I am most glad I tested instead of assumed.

OpenSearch makes new data searchable once per second ( refresh_interval ), and the document writer in my prototype used refresh=false . Reading a single document by id is real-time, but a listing or search can return the old version for up to a second after a write.

On its own that is a small delay. Together with Shopware’s HTTP cache it becomes a bug. I changed a product name and invalidated the cache right away, the way Shopware does after a write when its invalidation delay is turned off. Then I requested the category page:

  1. The cache was empty, so the page was rendered. The product link got its new SEO URL from MySQL, but the name, image alt text and tracking data in the same link came from the old document in OpenSearch.
  2. The HTTP cache stored that mixed page.
  3. A second request 1.3 seconds later, after OpenSearch had refreshed, still got the old name, now straight from the cache.

That happened in 5 of 5 tries. Store API listings right after a write returned the old name in 9 of 10 tries.

The fixes are known. Either invalidate the HTTP cache only after OpenSearch has refreshed, for example from the worker that writes the document. Or keep Shopware’s delayed cache invalidation, which is on by default and purges every five minutes, well after the refresh. Or, for rare admin writes only, wait for the refresh ( refresh=wait_for ), which made writes take about 0.86 s but brought stale Store API listings down to zero. What matters is that anyone building this knows the trap exists.

Where this leadsLink to this section

Put together, this is the architecture I would aim for:

flowchart LR
    W["Admin API, imports"] --> DB[("MySQL or MariaDB")]
    DB -->|"indexer and stock events"| P["Projector<br/>builds product documents"]
    P --> OS[("OpenSearch<br/>search + _source")]
    P -.->|"optional"| V[("Valkey<br/>reads by id")]
    S["Storefront and Store API"] -->|"search, listing"| OS
    S -.->|"detail, known ids"| V
    S -->|"cart, checkout, stock"| DB

Writes and everything that must be exact, like cart, checkout and stock validation, stay on the relational database, through the DAL and DAL v2. Storefront product reads come from documents. One projector connects the two sides.

None of this needs PostgreSQL. It helps every shop on the database it runs today. That is why I would do it in this order:

  1. MySQL hygiene and DAL v2. The findings from part 2 and the levers above. Small steps, no API break, gains for everyone.
  2. The read store, in parallel. It does not depend on the database question at all. It is still a draft and not filed upstream yet.
  3. The database groundwork, and PostgreSQL only if someone makes the business case.

A few honest limits: every timing comes from one developer machine, so the ratios are the result and the absolute numbers are not. The effort estimates are judgements, not measurements. And the part that reads documents from OpenSearch is a proof of concept without tests. The whole picture, with all the open questions, is in the target architecture doc.

TL;DRLink to this section

  • Keep the DAL. Doctrine ORM and Cycle ORM lack translations, inheritance and versioning, Doctrine cannot add plugin fields to core entities, and a switch would cost 100 to 230 person-weeks plus a break for every plugin.
  • DAL v2 makes reads 1.7x to 2.4x faster behind the same API, for 11 to 27 person-weeks.
  • At 150,000 products, serving the product document from OpenSearch gives 1.42x the throughput with 84 % less MySQL CPU. Valkey helps for reads by id, not for search.
  • JSON in SQL, MongoDB and a query layer for all engines were tested and dropped.
  • Watch the cache trap: invalidating the HTTP cache before OpenSearch refreshes stores stale pages.
  • None of it needs a new database. The biggest win was asking the database less, not picking a different one.

Looking backLink to this section

I started with “Why doesn’t Shopware run on PostgreSQL?” and ended up with a list of things that make Shopware better on MySQL, a clear “no” to an ORM and a read store that has nothing to do with PostgreSQL. I would ask the question again, and I would expect to be surprised again.

Comments 💬 & Reactions ✨