diff --git a/lib/base.php b/lib/base.php
index afa498e502e5ccbad4decb6624601c48e82ecb6b..6751f897dfcdd1ee671889524a28b96c704bd23f 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -475,16 +475,9 @@ class OC {
 		@ini_set('file_uploads', '50');
 
 		self::handleAuthHeaders();
-
 		self::initPaths();
-		if (OC_Config::getValue('instanceid', false)) {
-			// \OC\Memcache\Cache has a hidden dependency on
-			// OC_Util::getInstanceId() for namespacing. See #5409.
-			try {
-				self::$loader->setMemoryCache(\OC\Memcache\Factory::createLowLatency('Autoloader'));
-			} catch (\Exception $ex) {
-			}
-		}
+		self::registerAutoloaderCache();
+
 		OC_Util::isSetLocaleWorking();
 
 		// setup 3rdparty autoloader
@@ -644,6 +637,23 @@ class OC {
 		}
 	}
 
+	protected static function registerAutoloaderCache() {
+		// The class loader takes an optional low-latency cache, which MUST be
+		// namespaced. The instanceid is used for namespacing, but might be
+		// unavailable at this point. Futhermore, it might not be possible to
+		// generate an instanceid via \OC_Util::getInstanceId() because the
+		// config file may not be writable. As such, we only register a class
+		// loader cache if instanceid is available without trying to create one.
+		$instanceId = OC_Config::getValue('instanceid', null);
+		if ($instanceId) {
+			try {
+				$memcacheFactory = new \OC\Memcache\Factory($instanceId);
+				self::$loader->setMemoryCache($memcacheFactory->createLowLatency('Autoloader'));
+			} catch (\Exception $ex) {
+			}
+		}
+	}
+
 	/**
 	 * Handle the request
 	 */
diff --git a/lib/private/memcache/factory.php b/lib/private/memcache/factory.php
index d60b157efe240a40a0c154813d9b2be80f06c08e..8e47a8899fcab9e3ac0409c36534abffd2f5d135 100644
--- a/lib/private/memcache/factory.php
+++ b/lib/private/memcache/factory.php
@@ -59,7 +59,8 @@ class Factory implements ICacheFactory {
 	 * @param string $prefix
 	 * @return null|Cache
 	 */
-	public static function createLowLatency($prefix = '') {
+	public function createLowLatency($prefix = '') {
+		$prefix = $this->globalPrefix . '/' . $prefix;
 		if (XCache::isAvailable()) {
 			return new XCache($prefix);
 		} elseif (APCu::isAvailable()) {
@@ -76,7 +77,7 @@ class Factory implements ICacheFactory {
 	 *
 	 * @return bool
 	 */
-	public static function isAvailableLowLatency() {
+	public function isAvailableLowLatency() {
 		return XCache::isAvailable() || APCu::isAvailable() || APC::isAvailable();
 	}