Black Friday: the Laravel testing course is $149 instead of $249, through Dec 7. See the course →
logo

Will running MySQL in memory speed up your tests?

I measured it on this site's test suite

Joel Clermont
Joel Clermont
2026-12-01

From our daily Laravel tips email. Get the next one in your inbox.

In an earlier tip, I explained why we don't use SQLite for tests. The usual argument for SQLite is speed, since the whole database can live in memory.

But MySQL can live in memory too if we set it up that way. For example, if it runs in Docker, you can mount the data directory as a tmpfs volume, which is backed by RAM instead of disk:

# docker-compose.yml
services:
  mysql:
    image: mysql:8.4
    tmpfs:
      - /var/lib/mysql:rw,noexec,nosuid,size=1g

This doesn't change any behavior in MySQL, it just writes entirely in memory and never hits the disk.

So the real question: Does this make it faster?

I'm a man of science, so I tried it on the test suite for this site, which has 745 tests.

On a normal Docker volume, the suite averaged 1 minute 13 seconds. On tmpfs, it averaged 1 minute 12 seconds.

That was extremely underwhelming. Basically, there is no difference in speed at all. Why?

The reason is the RefreshDatabase trait. It wraps each test in a transaction and rolls it back at the end. And the commit is when MySQL writes the data to disk. That's the slow part, and this trait is bypassing it altogether.

To test that, I swapped it for the DatabaseTruncation trait, which does commit every write, but then cleans out the tables between tests. On the normal volume, the suite slowed down to 1 minute 25 seconds. On tmpfs, it came back down to 1 minute 15 seconds.

So it is faster, but still not that much.

Just to push the concept and see if I could evoke a bigger difference, outside a test, I ran 3,000 single-row inserts against the normal volume. Committing each row one at a time, the total operation took between 3 and 8 seconds. Here is where the in-memory disk shined. It dropped that same operation to less than 0.4 seconds.

So running MySQL in memory only helps tests that commit their writes, and even then it's a marginal improvement unless your tests do a LOT of separate writes.

My conclusion is that I'm not changing my Docker setup to use the in-memory disk. I'm already using the RefreshDatabase trait so every test starts clean and no test can leak data into another. But now, I know this is also speeding up my tests as well.

Here to help,

Joel

P.S. Our testing course covers the database side of testing too, from writing factories to making precise database assertions.

Want a tip like this in your inbox every weekday?

Each one takes less than 2 minutes to read. Join 4,000+ Laravel developers who already get them.

email
No spam. Only real-world advice.
Toss a coin in the jar if you found this helpful.