---
title: "Cache-aside pattern"
description: "Implement the cache-aside pattern using Aerospike as a high-performance cache layer with Java and Python examples."
---

# Cache-aside pattern

> For the complete documentation index see: [llms.txt](https://aerospike.com/docs/llms.txt)
> 
> All documentation pages available in markdown.

Implement the cache-aside (lazy-loading) pattern using Aerospike as your cache layer.

## Use case

Reduce database load by caching:

-   Expensive query results
-   Frequently accessed reference data
-   Computed aggregations

## How it works

```plaintext
┌─────────────┐    1. Check cache    ┌─────────────┐

│ Application │ ──────────────────── │  Aerospike  │

│             │                      │   (Cache)   │

│             │ ◄─────────────────── │             │

│             │    2. Cache hit?     │             │

│             │                      └─────────────┘

│             │

│             │    3. Cache miss     ┌─────────────┐

│             │ ──────────────────── │  Database   │

│             │                      │  (Source)   │

│             │ ◄─────────────────── │             │

│             │    4. Get data       └─────────────┘

│             │

│             │    5. Update cache   ┌─────────────┐

│             │ ──────────────────── │  Aerospike  │

└─────────────┘                      └─────────────┘
```

## Quick implementation

::: deserialization risk
The examples below use Java native serialization (`ObjectInputStream`/`ObjectOutputStream`) and Python `pickle` to keep the demo short. Both formats execute arbitrary code on deserialization (CWE-502) if an attacker can write to the cache set — for example through another service or tenant with write access to the same namespace/set. Never deserialize with `ObjectInputStream`/`pickle` on a set that untrusted callers can write to. For production use, serialize with a safe, data-only format such as JSON or Protobuf, or add serialization filtering (Java’s `ObjectInputFilter`) / a restricted unpickler (Python) if you must keep a binary format.
:::

-   [Java](#tab-panel-5304)
-   [Python](#tab-panel-5305)

```java
import com.aerospike.client.sdk.Cluster;

import com.aerospike.client.sdk.DataSet;

import com.aerospike.client.sdk.Record;

import com.aerospike.client.sdk.RecordResult;

import com.aerospike.client.sdk.RecordStream;

import com.aerospike.client.sdk.Session;

import com.aerospike.client.sdk.policy.Behavior;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

import java.util.Optional;

import java.util.function.Function;

public class CacheAside<T extends Serializable> {

    private final Session session;

    private final DataSet cache;

    private final Function<String, T> loader;

    private final int ttlSeconds;

    public CacheAside(Cluster cluster, String cacheName,

                      Function<String, T> loader, int ttlSeconds) {

        this.session = cluster.createSession(Behavior.DEFAULT);

        this.cache = DataSet.of("test", cacheName);

        this.loader = loader;

        this.ttlSeconds = ttlSeconds;

    }

    public T get(String key) {

        // 1. Check cache

        RecordStream stream = session.query(cache.id(key)).execute();

        Optional<Record> cached = stream.getFirst()

            .filter(RecordResult::isOk)

            .map(RecordResult::recordOrThrow);

        if (cached.isPresent()) {

            // 2. Cache hit

            return deserialize(cached.get().getBytes("data"));

        }

        // 3. Cache miss - load from source

        T value = loader.apply(key);

        // 4. Update cache

        session.insert(cache.id(key))

            .bin("data").setTo(serialize(value))

            .expireRecordAfterSeconds(ttlSeconds)

            .execute();

        return value;

    }

    public void invalidate(String key) {

        session.delete(cache.id(key)).execute().close();

    }

    private byte[] serialize(T value) {

        try (ByteArrayOutputStream bout = new ByteArrayOutputStream();

             ObjectOutputStream out = new ObjectOutputStream(bout)) {

            out.writeObject(value);

            out.flush();

            return bout.toByteArray();

        } catch (IOException e) {

            throw new RuntimeException("Failed to serialize cached value", e);

        }

    }

    @SuppressWarnings("unchecked")

    private T deserialize(byte[] bytes) {

        try (ByteArrayInputStream bin = new ByteArrayInputStream(bytes);

             ObjectInputStream in = new ObjectInputStream(bin)) {

            return (T) in.readObject();

        } catch (IOException | ClassNotFoundException e) {

            throw new RuntimeException("Failed to deserialize cached value", e);

        }

    }

}
```

> 📖 **API reference**: [`Cluster.createSession(Behavior)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/Cluster.html#createSession%28com.aerospike.client.sdk.policy.Behavior%29) | [`DataSet.of(...)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/DataSet.html#of%28java.lang.String%2Cjava.lang.String%29) | [`Session.insert(DataSet)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/Session.html#insert%28com.aerospike.client.sdk.DataSet%29) | [`Session.delete(Key)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/Session.html#delete%28com.aerospike.client.sdk.Key%29) | [`Session.query(Key)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/Session.html#query%28com.aerospike.client.sdk.Key%29) | [`ChainableQueryBuilder.bin(...)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/ChainableQueryBuilder.html#bin%28java.lang.String%29) | [`ChainableQueryBuilder.execute()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/ChainableQueryBuilder.html#execute%28%29) | [`ChainableNoBinsBuilder.execute()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/ChainableNoBinsBuilder.html#execute%28%29) | [`RecordStream.getFirst()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/RecordStream.html#getFirst%28%29) | [`RecordStream.close()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/RecordStream.html#close%28%29) | [`RecordResult.isOk()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/RecordResult.html#isOk%28%29)

```python
from typing import Callable, TypeVar

import pickle

from aerospike_sdk import Behavior, DataSet

T = TypeVar('T')

class CacheAside:

    def __init__(self, cluster, cache_name: str,

                 loader: Callable[[str], T], ttl_seconds: int):

        self.session = cluster.create_session(Behavior.DEFAULT)

        self.cache = DataSet.of("test", cache_name)

        self.loader = loader

        self.ttl_seconds = ttl_seconds

    async def get(self, key: str) -> T:

        # 1. Check cache

        stream = await self.session.query(self.cache.id(key)).execute()

        row = await stream.first()

        cached = row.record_or_raise() if row is not None and row.is_ok and row.record is not None else None

        stream.close()

        if cached:

            # 2. Cache hit

            return pickle.loads(cached.bins.get("data"))

        # 3. Cache miss - load from source

        value = self.loader(key)

        # 4. Update cache

        await (

            self.session.insert(self.cache.id(key))

            .put({"data": pickle.dumps(value)})

            .expire_record_after_seconds(self.ttl_seconds)

            .execute()

        )

        return value

    async def invalidate(self, key: str):

        await self.session.delete(self.cache.id(key)).execute()
```

> 📖 **API reference**: [`DataSet.of()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/dataset.html#aerospike%5Fsdk.dataset.DataSet.of) | [`DataSet.id()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/dataset.html#aerospike%5Fsdk.dataset.DataSet.id) | [`Behavior.DEFAULT`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/behavior.html#aerospike%5Fsdk.policy.behavior.Behavior.DEFAULT) | [`Session.query()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/session.html#aerospike%5Fsdk.aio.session.Session.query) | [`Session.insert()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/session.html#aerospike%5Fsdk.aio.session.Session.insert) | [`Session.delete()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/session.html#aerospike%5Fsdk.aio.session.Session.delete) | [`WriteSegmentBuilder.put()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/write-segment.html#aerospike%5Fsdk.aio.operations.query.WriteSegmentBuilder.put) | [`WriteSegmentBuilder.expire_record_after_seconds()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/write-segment.html#aerospike%5Fsdk.aio.operations.query.WriteSegmentBuilder.expire%5Frecord%5Fafter%5Fseconds) | [`RecordResult.record_or_raise()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/record-result.html#aerospike%5Fsdk.record%5Fresult.RecordResult.record%5For%5Fraise) | [`RecordStream.first()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/record-stream.html#aerospike%5Fsdk.record%5Fstream.RecordStream.first) | [`RecordStream.close()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/record-stream.html#aerospike%5Fsdk.record%5Fstream.RecordStream.close) | [`QueryBuilder.execute()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/query.html#aerospike%5Fsdk.aio.operations.query.QueryBuilder.execute) | [`WriteSegmentBuilder.execute()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/write-segment.html#aerospike%5Fsdk.aio.operations.query.WriteSegmentBuilder.execute)

## Usage

-   [Java](#tab-panel-5306)
-   [Python](#tab-panel-5307)

```java
import com.aerospike.client.sdk.Cluster;

import com.aerospike.client.sdk.ClusterDefinition;

import java.io.Serializable;

import java.util.Map;

record User(String id, String name) implements Serializable {}

Map<String, User> database = Map.of(

    "user-123", new User("user-123", "Alice")

);

try (Cluster cluster = new ClusterDefinition("localhost", 3000).connect()) {

    // Create cache with database loader

    CacheAside<User> userCache = new CacheAside<>(

        cluster,

        "cache_users",

        userId -> database.get(userId),  // Loader function

        3600  // 1 hour TTL

    );

    // Get user - checks cache first, then source map

    User user = userCache.get("user-123");

    System.out.println("Loaded user: " + user);

    // Invalidate on update

    userCache.invalidate("user-123");

}
```

> 📖 **API reference**: [`ClusterDefinition(String,int)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/ClusterDefinition.html#%3Cinit%3E%28java.lang.String%2Cint%29) | [`ClusterDefinition.connect()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/ClusterDefinition.html#connect%28%29) | [`Cluster.close()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/Cluster.html#close%28%29)

```python
import asyncio

from aerospike_sdk import ClusterDefinition

database = {

    "user-123": {"id": "user-123", "name": "Alice"}

}

async def main():

    async with ClusterDefinition("localhost", 3000).connect() as cluster:

        # Create cache with database loader

        user_cache = CacheAside(

            cluster,

            cache_name="cache_users",

            loader=lambda user_id: database.get(user_id),

            ttl_seconds=3600,  # 1 hour TTL

        )

        # Get user - checks cache first, then source map

        user = await user_cache.get("user-123")

        print(f"Loaded user: {user}")

        # Invalidate on update

        await user_cache.invalidate("user-123")

if __name__ == "__main__":

    asyncio.run(main())
```

> 📖 **API reference**: [`ClusterDefinition`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/cluster-definition.html#aerospike%5Fsdk.aio.cluster%5Fdefinition.ClusterDefinition)

## Complete example

-   [Java](#tab-panel-5308)
-   [Python](#tab-panel-5309)

```java
import com.aerospike.client.sdk.AerospikeException;

import com.aerospike.client.sdk.Cluster;

import com.aerospike.client.sdk.ClusterDefinition;

import com.aerospike.client.sdk.DataSet;

import com.aerospike.client.sdk.Record;

import com.aerospike.client.sdk.RecordResult;

import com.aerospike.client.sdk.RecordStream;

import com.aerospike.client.sdk.ResultCode;

import com.aerospike.client.sdk.Session;

import com.aerospike.client.sdk.policy.Behavior;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

import java.util.Map;

import java.util.Optional;

import java.util.function.Function;

public class CacheAsideExample {

    record User(String id, String name) implements Serializable {}

    static class CacheAside<T extends Serializable> {

        private final Session session;

        private final DataSet cache;

        private final Function<String, T> loader;

        private final int ttlSeconds;

        CacheAside(Cluster cluster, String setName, Function<String, T> loader, int ttlSeconds) {

            this.session = cluster.createSession(Behavior.DEFAULT);

            this.cache = DataSet.of("test", setName);

            this.loader = loader;

            this.ttlSeconds = ttlSeconds;

        }

        T get(String key) {

            RecordStream read = session.query(cache.id(key)).execute();

            Optional<Record> cached;

            try {

                cached = read.getFirst()

                    .filter(RecordResult::isOk)

                    .map(RecordResult::recordOrThrow);

            } finally {

                read.close();

            }

            if (cached.isPresent()) {

                return deserialize(cached.get().getBytes("data"));

            }

            T value = loader.apply(key);

            if (value == null) {

                return null;

            }

            try {

                session.insert(cache.id(key))

                    .bin("data").setTo(serialize(value))

                    .expireRecordAfterSeconds(ttlSeconds)

                    .execute();

            } catch (AerospikeException e) {

                if (e.getResultCode() != ResultCode.FAIL_FORBIDDEN) {

                    throw e;

                }

                session.insert(cache.id(key))

                    .bin("data").setTo(serialize(value))

                    .execute();

            }

            return value;

        }

        void invalidate(String key) {

            session.delete(cache.id(key)).execute().close();

        }

        private byte[] serialize(T value) {

            try (ByteArrayOutputStream bout = new ByteArrayOutputStream();

                 ObjectOutputStream out = new ObjectOutputStream(bout)) {

                out.writeObject(value);

                out.flush();

                return bout.toByteArray();

            } catch (IOException e) {

                throw new RuntimeException("Failed to serialize cache value", e);

            }

        }

        @SuppressWarnings("unchecked")

        private T deserialize(byte[] data) {

            try (ByteArrayInputStream bin = new ByteArrayInputStream(data);

                 ObjectInputStream in = new ObjectInputStream(bin)) {

                return (T) in.readObject();

            } catch (IOException | ClassNotFoundException e) {

                throw new RuntimeException("Failed to deserialize cache value", e);

            }

        }

    }

    public static void main(String[] args) {

        Map<String, User> database = Map.of("user-123", new User("user-123", "Alice"));

        try (Cluster cluster = new ClusterDefinition("localhost", 3000).connect()) {

            CacheAside<User> cache = new CacheAside<>(cluster, "cache_users", database::get, 3600);

            User first = cache.get("user-123");   // miss -> load + cache

            User second = cache.get("user-123");  // hit

            System.out.println("Loaded user (miss): " + first);

            System.out.println("Loaded user (hit): " + second);

            cache.invalidate("user-123");

        }

    }

}
```

> 📖 **API reference**: [`ClusterDefinition(String,int)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/ClusterDefinition.html#%3Cinit%3E%28java.lang.String%2Cint%29) | [`ClusterDefinition.connect()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/ClusterDefinition.html#connect%28%29) | [`Cluster.createSession(Behavior)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/Cluster.html#createSession%28com.aerospike.client.sdk.policy.Behavior%29) | [`Cluster.close()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/Cluster.html#close%28%29) | [`DataSet.of(...)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/DataSet.html#of%28java.lang.String%2Cjava.lang.String%29) | [`Session.insert(DataSet)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/Session.html#insert%28com.aerospike.client.sdk.DataSet%29) | [`Session.delete(Key)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/Session.html#delete%28com.aerospike.client.sdk.Key%29) | [`Session.query(Key)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/Session.html#query%28com.aerospike.client.sdk.Key%29) | [`ChainableQueryBuilder.bin(...)`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/ChainableQueryBuilder.html#bin%28java.lang.String%29) | [`ChainableQueryBuilder.execute()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/ChainableQueryBuilder.html#execute%28%29) | [`ChainableNoBinsBuilder.execute()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/ChainableNoBinsBuilder.html#execute%28%29) | [`RecordStream.getFirst()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/RecordStream.html#getFirst%28%29) | [`RecordStream.close()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/RecordStream.html#close%28%29) | [`RecordResult.isOk()`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/RecordResult.html#isOk%28%29) | [`AerospikeException`](https://javadoc.io/doc/com.aerospike/aerospike-client-sdk/latest/com/aerospike/client/sdk/AerospikeException.html)

```python
import asyncio

import pickle

from typing import Callable, TypeVar

from aerospike_sdk import AerospikeError, Behavior, ClusterDefinition, DataSet, ResultCode

T = TypeVar("T")

class CacheAside:

    def __init__(self, cluster, cache_name: str, loader: Callable[[str], T], ttl_seconds: int):

        self.session = cluster.create_session(Behavior.DEFAULT)

        self.cache = DataSet.of("test", cache_name)

        self.loader = loader

        self.ttl_seconds = ttl_seconds

    async def get(self, key: str) -> T | None:

        stream = await self.session.query(self.cache.id(key)).execute()

        row = await stream.first()

        if row is not None and row.is_ok and row.record is not None:

            return pickle.loads(row.record_or_raise().bins.get("data"))

        value = self.loader(key)

        if value is None:

            return None

        payload = {"data": pickle.dumps(value)}

        try:

            await (

                self.session.insert(self.cache.id(key)).put(payload)

                .expire_record_after_seconds(self.ttl_seconds)

                .execute()

            )

        except AerospikeError as e:

            if e.result_code != ResultCode.FAIL_FORBIDDEN:

                raise

            await self.session.insert(self.cache.id(key)).put(payload).execute()

        return value

    async def invalidate(self, key: str):

        await self.session.delete(self.cache.id(key)).execute()

async def main():

    database = {"user-123": {"id": "user-123", "name": "Alice"}}

    async with ClusterDefinition("localhost", 3000).connect() as cluster:

        user_cache = CacheAside(

            cluster,

            cache_name="cache_users",

            loader=lambda user_id: database.get(user_id),

            ttl_seconds=3600,

        )

        user1 = await user_cache.get("user-123")  # miss -> load + cache

        user2 = await user_cache.get("user-123")  # hit

        print(f"Loaded user (miss): {user1}")

        print(f"Loaded user (hit): {user2}")

        await user_cache.invalidate("user-123")

if __name__ == "__main__":

    asyncio.run(main())
```

> 📖 **API reference**: [`ClusterDefinition`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/cluster-definition.html#aerospike%5Fsdk.aio.cluster%5Fdefinition.ClusterDefinition) | [`Cluster.create_session()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/cluster.html#aerospike%5Fsdk.aio.cluster.Cluster.create%5Fsession) | [`DataSet.of()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/dataset.html#aerospike%5Fsdk.dataset.DataSet.of) | [`DataSet.id()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/dataset.html#aerospike%5Fsdk.dataset.DataSet.id) | [`Behavior.DEFAULT`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/behavior.html#aerospike%5Fsdk.policy.behavior.Behavior.DEFAULT) | [`Session.query()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/session.html#aerospike%5Fsdk.aio.session.Session.query) | [`Session.insert()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/session.html#aerospike%5Fsdk.aio.session.Session.insert) | [`Session.delete()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/session.html#aerospike%5Fsdk.aio.session.Session.delete) | [`WriteSegmentBuilder.put()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/write-segment.html#aerospike%5Fsdk.aio.operations.query.WriteSegmentBuilder.put) | [`WriteSegmentBuilder.expire_record_after_seconds()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/write-segment.html#aerospike%5Fsdk.aio.operations.query.WriteSegmentBuilder.expire%5Frecord%5Fafter%5Fseconds) | [`RecordResult.record_or_raise()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/record-result.html#aerospike%5Fsdk.record%5Fresult.RecordResult.record%5For%5Fraise) | [`RecordStream.first()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/record-stream.html#aerospike%5Fsdk.record%5Fstream.RecordStream.first) | [`RecordStream.close()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/record-stream.html#aerospike%5Fsdk.record%5Fstream.RecordStream.close) | [`QueryBuilder.execute()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/query.html#aerospike%5Fsdk.aio.operations.query.QueryBuilder.execute) | [`WriteSegmentBuilder.execute()`](https://aerospike-python-sdk.readthedocs.io/en/latest/api/write-segment.html#aerospike%5Fsdk.aio.operations.query.WriteSegmentBuilder.execute)

### Expected output

-   [Java](#tab-panel-5310)
-   [Python](#tab-panel-5311)

```text
Loaded user (miss): User[id=user-123, name=Alice]

Loaded user (hit): User[id=user-123, name=Alice]
```

```text
Loaded user (miss): {'id': 'user-123', 'name': 'Alice'}

Loaded user (hit): {'id': 'user-123', 'name': 'Alice'}
```

## Cache invalidation strategies

| Strategy | Description | When to Use |
| --- | --- | --- |
| TTL expiration | Data expires after fixed time | Acceptable staleness |
| Write-through | Update cache on every write | Consistency required |
| Event-driven | Invalidate on change events | Event system available |

## Next steps

Session Store

Build high-velocity session storage.

[Session Store →](https://aerospike.com/docs/develop/client/sdk/recipes/session-store)

Rate Limiting

Implement distributed rate limiting.

[Rate Limiting →](https://aerospike.com/docs/develop/client/sdk/recipes/rate-limiting)