diff --git a/modules/search_api_db/src/DatabaseCompatibility/DatabaseCompatibilityHandlerInterface.php b/modules/search_api_db/src/DatabaseCompatibility/DatabaseCompatibilityHandlerInterface.php
index 2a3d36f3..d96431d5 100644
--- a/modules/search_api_db/src/DatabaseCompatibility/DatabaseCompatibilityHandlerInterface.php
+++ b/modules/search_api_db/src/DatabaseCompatibility/DatabaseCompatibilityHandlerInterface.php
@@ -2,6 +2,8 @@
 
 namespace Drupal\search_api_db\DatabaseCompatibility;
 
+use Drupal\Core\Database\Connection;
+
 /**
  * Bundles methods for resolving DBMS-specific differences.
  *
@@ -19,6 +21,17 @@
    */
   public function getDatabase();
 
+  /**
+   * Creates a clone of this service for the given database.
+   *
+   * @param \Drupal\Core\Database\Connection $database
+   *   A database of a type compatible with this class.
+   *
+   * @return static
+   *   A clone of this service class for the given database.
+   */
+  public function getCloneForDatabase(Connection $database);
+
   /**
    * Reacts to a new table being created.
    *
diff --git a/modules/search_api_db/src/DatabaseCompatibility/GenericDatabase.php b/modules/search_api_db/src/DatabaseCompatibility/GenericDatabase.php
index c6e02340..c5de9250 100644
--- a/modules/search_api_db/src/DatabaseCompatibility/GenericDatabase.php
+++ b/modules/search_api_db/src/DatabaseCompatibility/GenericDatabase.php
@@ -45,6 +45,15 @@ public function getDatabase() {
     return $this->database;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getCloneForDatabase(Connection $database) {
+    $service = clone $this;
+    $service->database = $database;
+    return $service;
+  }
+
   /**
    * {@inheritdoc}
    */
diff --git a/modules/search_api_db/src/Plugin/search_api/backend/Database.php b/modules/search_api_db/src/Plugin/search_api/backend/Database.php
index 1eb19991..6826bc15 100644
--- a/modules/search_api_db/src/Plugin/search_api/backend/Database.php
+++ b/modules/search_api_db/src/Plugin/search_api/backend/Database.php
@@ -197,12 +197,23 @@ public static function create(ContainerInterface $container, array $configuratio
 
     // For a new backend plugin, the database might not be set yet. In that case
     // we of course also don't need a DBMS compatibility handler.
-    if ($backend->getDatabase()) {
+    $database = $backend->getDatabase();
+    if ($database) {
       $dbms_compatibility_handler = $container->get('search_api_db.database_compatibility');
       // Make sure that we actually provide a handler for the right database,
-      // otherwise fall back to the generic handler.
-      if ($dbms_compatibility_handler->getDatabase() != $backend->getDatabase()) {
-        $dbms_compatibility_handler = new GenericDatabase($backend->getDatabase(), $container->get('transliteration'));
+      // otherwise create the right service manually. (This is the case if the
+      // user didn't pick the default database.)
+      if ($dbms_compatibility_handler->getDatabase() != $database) {
+        $database_type = $database->databaseType();
+        $service_id = "$database_type.search_api_db.database_compatibility";
+        if ($container->has($service_id)) {
+          /** @var \Drupal\search_api_db\DatabaseCompatibility\DatabaseCompatibilityHandlerInterface $dbms_compatibility_handler */
+          $dbms_compatibility_handler = $container->get($service_id);
+          $dbms_compatibility_handler = $dbms_compatibility_handler->getCloneForDatabase($database);
+        }
+        else {
+          $dbms_compatibility_handler = new GenericDatabase($database, $container->get('transliteration'));
+        }
       }
       $backend->setDbmsCompatibilityHandler($dbms_compatibility_handler);
     }
@@ -358,6 +369,16 @@ public function setDataTypeHelper(DataTypeHelper $data_type_helper) {
     return $this;
   }
 
+  /**
+   * Retrieves the DBMS compatibility handler.
+   *
+   * @return \Drupal\search_api_db\DatabaseCompatibility\DatabaseCompatibilityHandlerInterface
+   *   The DBMS compatibility handler.
+   */
+  public function getDbmsCompatibilityHandler() {
+    return $this->dbmsCompatibility;
+  }
+
   /**
    * Sets the DBMS compatibility handler.
    *
diff --git a/modules/search_api_db/tests/src/Kernel/BackendTest.php b/modules/search_api_db/tests/src/Kernel/BackendTest.php
index 7505d01f..8d928d4b 100644
--- a/modules/search_api_db/tests/src/Kernel/BackendTest.php
+++ b/modules/search_api_db/tests/src/Kernel/BackendTest.php
@@ -3,9 +3,11 @@
 namespace Drupal\Tests\search_api_db\Kernel;
 
 use Drupal\Component\Render\FormattableMarkup;
+use Drupal\Core\Database\Database as CoreDatabase;
 use Drupal\search_api\Entity\Server;
 use Drupal\search_api\Query\QueryInterface;
 use Drupal\search_api\SearchApiException;
+use Drupal\search_api_db\DatabaseCompatibility\GenericDatabase;
 use Drupal\search_api_db\Plugin\search_api\backend\Database;
 use Drupal\search_api_db\Tests\DatabaseTestsTrait;
 use Drupal\Tests\search_api\Kernel\BackendTestBase;
@@ -581,4 +583,58 @@ protected function getIndexDbInfo($index_id = NULL) {
       ->get($index_id);
   }
 
+  /**
+   * Tests whether a server on a non-default database is handled correctly.
+   */
+  public function testNonDefaultDatabase() {
+    // Clone the primary credentials to a replica connection.
+    // Note this will result in two independent connection objects that happen
+    // to point to the same place.
+    // @see \Drupal\KernelTests\Core\Database\ConnectionTest::testConnectionRouting()
+    $connection_info = CoreDatabase::getConnectionInfo('default');
+    CoreDatabase::addConnectionInfo('default', 'replica', $connection_info['default']);
+
+    $db1 = CoreDatabase::getConnection('default', 'default');
+    $db2 = CoreDatabase::getConnection('replica', 'default');
+
+    // Safety checks copied from the Core test, if these fail something is wrong
+    // with Core.
+    $this->assertNotNull($db1, 'default connection is a real connection object.');
+    $this->assertNotNull($db2, 'replica connection is a real connection object.');
+    $this->assertNotSame($db1, $db2, 'Each target refers to a different connection.');
+
+    // Create backends based on each of the two targets and verify they use the
+    // right connections.
+    $config = [
+      'database' => 'default:default',
+    ];
+    $backend1 = Database::create($this->container, $config, '', []);
+    $config['database'] = 'default:replica';
+    $backend2 = Database::create($this->container, $config, '', []);
+
+    $this->assertSame($db1, $backend1->getDatabase());
+    $this->assertSame($db2, $backend2->getDatabase());
+
+    // Make sure they also use different DBMS compatibility handlers, which also
+    // use the correct database connections.
+    $dbms_comp1 = $backend1->getDbmsCompatibilityHandler();
+    $dbms_comp2 = $backend2->getDbmsCompatibilityHandler();
+    $this->assertNotSame($dbms_comp1, $dbms_comp2);
+    $this->assertSame($db1, $dbms_comp1->getDatabase());
+    $this->assertSame($db2, $dbms_comp2->getDatabase());
+
+    // Finally, make sure the DBMS compatibility handlers also have the correct
+    // classes (meaning we used the correct one and didn't just fall back to the
+    // generic database).
+    $service = $this->container->get('search_api_db.database_compatibility');
+    $database_type = $db1->databaseType();
+    $service_id = "$database_type.search_api_db.database_compatibility";
+    $service2 = $this->container->get($service_id);
+    $this->assertSame($service2, $service);
+    $class = get_class($service);
+    $this->assertNotEquals(GenericDatabase::class, $class);
+    $this->assertSame($dbms_comp1, $service);
+    $this->assertEquals($class, get_class($dbms_comp2));
+  }
+
 }
