I have an application with multiple Azure SQL databases sharded by a key. Every time we open a shard database connection, we retrieve the ShardMapManager and the ListShardMap:
public static ShardMapManager CreateOrGetShardMapManager(string shardMapManagerConnectionString)
{
// Get shard map manager database connection string
// Try to get a reference to the Shard Map Manager in the Shard Map Manager database. If it doesn't already exist, then create it.
ShardMapManager shardMapManager = default(ShardMapManager);
bool shardMapManagerExists = ShardMapManagerFactory.TryGetSqlShardMapManager(shardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy, out shardMapManager);
if (!shardMapManagerExists)
{
// The Shard Map Manager does not exist, so create it
shardMapManager = ShardMapManagerFactory.CreateSqlShardMapManager(shardMapManagerConnectionString);
}
return shardMapManager;
}
public static ListShardMap<T> CreateOrGetListMap<T>(ShardMapManager shardMapManager, string shardMapName)
{
// Try to get a reference to the Shard Map.
ListShardMap<T> shardMap = null;
bool shardMapExists = shardMapManager.TryGetListShardMap(shardMapName, out shardMap);
if (!shardMapExists)
{
// The Shard Map does not exist, so create it
shardMap = shardMapManager.CreateListShardMap<T>(shardMapName);
}
return shardMap;
}
And then we open a connection by passing the shard key to OpenConnectionForKey()
:
public static DbConnection CreateShardConnection(int shardingKey, string connectionStr, string shardMapName)
{
var shardMapManager = CreateOrGetShardMapManager(connectionStr);
var shardMap = CreateOrGetListMap<int>(shardMapManager, shardMapName);
SqlConnection conn = shardMap.OpenConnectionForKey<int>(shardingKey, GenerateCredentialsConnString(connectionStr), ConnectionOptions.Validate);
return conn;
}
We only use one shard map and one list map for all the shards, so my question is - can we cache these objects in a global variable? Are they designed to be thread safe, and will it help performance?
Update
I found here https://learn.microsoft.com/en-us/azure/sql-database/sql-database-elastic-scale-shard-map-management#constructing-a-shardmapmanager that the ShardMapManager
should indeed be created once and stored in a global variable.
But what about the ListShardMap
? If we only have one, can we store that in a global variable too?