diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php
index 9a1b078dfa3429db9e2735e8c43b72d4c23c43da..7f56b8794a5559f343058cb1725d01dc28f3f221 100644
--- a/core/tests/Drupal/KernelTests/KernelTestBase.php
+++ b/core/tests/Drupal/KernelTests/KernelTestBase.php
@@ -12,6 +12,7 @@
 use Drupal\Core\DrupalKernel;
 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
 use Drupal\Core\Extension\ExtensionDiscovery;
+use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
 use Drupal\Core\Language\Language;
 use Drupal\Core\Site\Settings;
 use Drupal\Core\Test\TestDatabase;
@@ -201,7 +202,7 @@ abstract class KernelTestBase extends TestCase implements ServiceProviderInterfa
    *
    * @var \Drupal\Core\KeyValueStore\KeyValueMemoryFactory
    */
-  protected $keyValue;
+  protected KeyValueMemoryFactory $keyValue;
 
   /**
    * The app root.
@@ -561,18 +562,14 @@ public function register(ContainerBuilder $container) {
       ->register('lock', 'Drupal\Core\Lock\NullLockBackend');
     $container
       ->register('cache_factory', 'Drupal\Core\Cache\MemoryBackendFactory');
-    $container
-      ->register('keyvalue.memory', 'Drupal\Core\KeyValueStore\KeyValueMemoryFactory');
-    $container
-      ->setAlias('keyvalue', 'keyvalue.memory');
 
-    // Must persist container rebuilds, or all data would vanish otherwise.
-    if ($this->keyValue !== NULL) {
-      $container->set('keyvalue.memory', $this->keyValue);
-    }
-    else {
-      $this->keyValue = $container->get('keyvalue.memory');
+    // Use memory for key value storages to avoid database queries. Store the
+    // key value factory on the test object so that key value storages persist
+    // container rebuilds, otherwise all state data would vanish.
+    if (!isset($this->keyValue)) {
+      $this->keyValue = new KeyValueMemoryFactory();
     }
+    $container->set('keyvalue', $this->keyValue);
 
     // Set the default language on the minimal container.
     $container->setParameter('language.default_values', Language::$defaultValues);