How do I overwrite a lazy service? I'm trying to have my D8 site use Memcache for the lock service backend instead of using MySQL which is the default.

In core.services.yml we have the lock defintions:

  lock:
    class: Drupal\Core\Lock\DatabaseLockBackend
    arguments: ['@database']
    tags:
      - { name: backend_overridable }
    lazy: true
  lock.persistent:
    class: Drupal\Core\Lock\PersistentDatabaseLockBackend
    arguments: ['@database']
    tags:
      - { name: backend_overridable }
    lazy: true


In my site's default.services.yml I have this:

services:
  # Replaces the default lock backend with a memcache implementation.
  lock:
    class: Drupal\Core\Lock\LockBackendInterface
    factory: ['@memcache.lock.factory', get]


But when I obtain the lock service via \Drupal::service('lock');, I get a class Drupal\Core\ProxyClass\Lock\DatabaseLockBackend returned instead of an instance of Drupal\memcache\Lock\MemcacheLockBackend. So my override did not work.

Unfortunately there's no documentation in the D8 memcache module for doing this, I'm just kind of winging it.

Perhaps it has something to do with the fact that the original service is lazy loaded?
default.services.yml is just basically just documentation/ an example, it's not actually loaded, at least not by default.

To load a services.yml file, it either needs to be named services.yml, or you need to explicitly added to the settings.php file, see example.settings.local.php and how it loads development services.

The redis module also provides implementations for those services, and it does so by providing a separate example.services.yml file that you can either include directly or copy and customize. That definitely works, I wrote and tested it.