Sitecore XDB -Moving Shard database across environments

I have recently been involved with some tasks to migrate the XDB database across different environments and I have discovered few interesting things about XDB shard databases that I did not know about it….

First and more important things to know, the shard database from Sitecore 9 onwards is supported by 3 databases:

  1. Shard Management
  2. Shard 0 DB
  3. Shard 1 Db

Shard database is used for the XDB collection – meaning that XDB and shard databases hold all customers data and interactions assuming that you have tracking enabled within your sitecore instance…

In case you are not aware this documentation explain well what is sharding and how it works and why it is required, long story short Sharding is a tecnique to increase scalability of your SQL database for non relational data structures… https://azure.microsoft.com/en-gb/resources/cloud-computing-dictionary/what-is-database-sharding#:~:text=Database%20sharding%20is%20a%20type,server%20instance%20to%20spread%20load.

SQL Scripts

From a sitecore Admin prospective what you need to know is that if you want to move a shard database across your sitecore environments you need to move the databases and once you have moved the databases in a new environment you need to remind to update the Shard management database (typically the naming is such as XXXXXXXX-smm-db in [__ShardManagement].[ShardsGlobal] table where the connection to the other shard database is kept.

This SQL script is basically helping you to amend the server and database name

update [__ShardManagement].[ShardsGlobal] set databasename='XXXX-shard0', servername='XXX-xp-rg-sql' where databasename='XXXXX-shard0-db'
update [__ShardManagement].[ShardsGlobal] set databasename=XXXX-shard1-db', servername='XXXX-xp-rg-sql' where databasename='XXXXX-shard1-db'

SOLR configuration

once you have updated your SQL connection string you have to updated SOLR XDB index to inform Solr of the change of the Shard configuration since Solr hold a cache to the shard databases…

The simplest way is just deleting the XDB-INDEX-TOKEN document that hold the connection string posting the following command on the SOLR XDB and XDB rebuild index

<delete><query>id:xdb-index-token</query></delete>

XDB Search Index Rebuild

Finally remember to deploy in different environment any custom XDB facets and you are ready to retrigger your XDB index rebuild from the indexing service using the following command from a kudu console

Sitecore.XConnectSearchIndexer.exe -rr

Here there is further documentation for XDB index rebuild in case you have no luck or missed any of the above steps

https://doc.sitecore.com/xp/en/developers/103/sitecore-experience-platform/rebuild-the-xdb-index-in-solr.html