Skip to content

Cache sync

Andrei Barbolin edited this page Dec 8, 2023 · 1 revision

There is a minimum configuration to set up cache sync

{
    "MemcachedConfiguration": {
        "HeadlessServiceAddress": "my-memchached-service-headless.namespace.svc.cluster.local",
        "SyncSettings": {
            "SyncServers": [
                {
                    "Address": "http://my-service.cluster1.k8s.net",
                    "ClusterName": "cluster1"
                },
                {
                    "Address": "http://my-service.cluster2.k8s.net",
                    "ClusterName": "cluster2"
                }
            ],
            "ClusterNameEnvVariable": "MY_ENV_VAR_FOR_CLUSTER_NAME" 
        }
    }
}

You can use it without ClusterName and ClusterNameEnvVariable but sync will try to sync data to all servers from SyncServers. Otherwise you need to make your own implementation of ISyncServersProvider and register it instead of DefaultSyncServersProvider

Add endpoint for each type which are used in your application to store cache data

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMemcached(builder.Configuration);

var app = builder.Build();

app.UseEndpoints(endpoints =>
        {
            endpoints.AddMemcachedSyncEndpoint<string>(builder.Configuration);
            endpoints.AddMemcachedSyncEndpoint<ComplexModel>(builder.Configuration);
            endpoints.AddMemcachedSyncEndpoint<List<string>>(builder.Configuration);
            endpoints.AddMemcachedSyncEndpoint<Dictionary<string, string>>(builder.Configuration);
            endpoints.AddMemcachedEndpoints(builder.Configuration);
            endpoints.MapControllers();
        });

Also check samples to understand how it works.

Add circuit breaker settings for possible cluster outages to stop sync

{
    "MemcachedConfiguration": {
        "HeadlessServiceAddress": "my-memchached-service-headless.namespace.svc.cluster.local",
        "SyncSettings": {
            "SyncServers": [
                {
                    "Address": "http://my-service.cluster1.k8s.net",
                    "ClusterName": "cluster1"
                },
                {
                    "Address": "http://my-service.cluster2.k8s.net",
                    "ClusterName": "cluster2"
                }
            ],
            "ClusterNameEnvVariable": "MY_ENV_VAR_FOR_CLUSTER_NAME",
            "CacheSyncCircuitBreaker": {
                "Interval": "00:01:00",
                "MaxErrors": 50,
                "SwitchOffTime": "00:02:00"
            }
        }
    }
}

After MaxErrors per pod logs about sync switch off will appear: Sync to {SererKey} is switched off until {SwitchOffThresholdTime}, reason: too many errors

It's better to use write-through approach with cache sync in order to have better hit ratio. Consider using Flush after cluster outage. It's possible that some updates are not synced within outage time and if expiration of old data is long enough you will have outdated data on one of the clusters until expiration is hit. After adding memcached endpoints service has it's own endpoint to memcached to flush data endpoints.AddMemcachedEndpoints(builder.Configuration);

Clone this wiki locally