MS Cache Documentation

Comprehensive guide to understanding and utilizing the MS Cache system.

Introduction to MS Cache

MS Cache is a high-performance, distributed in-memory data structure store designed for speed and scalability. It provides a robust solution for caching frequently accessed data, reducing latency, and improving application responsiveness. Whether you're dealing with session data, API responses, or database query results, MS Cache can significantly enhance your application's performance.

It is built with modern architectures in mind, offering features like automatic data expiration, configurable eviction strategies, and flexible persistence options.

Key Features

  • High Performance: Optimized for low-latency reads and writes.
  • Scalability: Designed to scale horizontally across multiple nodes.
  • Data Structures: Supports various data types including strings, lists, sets, hashes, and sorted sets.
  • Durability: Options for in-memory persistence and snapshotting.
  • Replication: Master-replica replication for high availability.
  • Extensibility: Modular design for custom modules.

Basic Usage

Configuration

MS Cache can be configured through a simple configuration file or environment variables. The default configuration file is typically named ms-cache.conf.

Here's an example of a basic configuration:


port 6379
maxmemory 2gb
maxmemory-policy allkeys-lru
save 900 1
save 300 10
save 60 10000
                

This configuration sets the port to 6379, limits memory usage to 2GB, and uses the LRU (Least Recently Used) eviction policy.

API Reference

MS Cache exposes a simple, yet powerful API. Below are some common operations:

Setting a Key-Value Pair


SET mykey "Hello MS Cache"
# Response: OK
                

Getting a Value by Key


GET mykey
# Response: "Hello MS Cache"
                

Setting a Key with Expiration


SETEX anotherkey 60 "This will expire in 60 seconds"
# Response: OK
                

Working with Hashes


HSET user:100 name "Alice" age 30
# Response: (integer) 2

HGET user:100 name
# Response: "Alice"
                

Advanced Concepts

Eviction Policies

When the cache reaches its memory limit, MS Cache needs to decide which keys to remove to make space for new ones. You can choose from several eviction policies:

  • noeviction: Return an error when the memory limit is reached.
  • allkeys-lru: Remove the least recently used (LRU) keys.
  • volatile-lru: Remove the least recently used (LRU) keys among those with an expire set.
  • allkeys-random: Remove random keys.
  • volatile-random: Remove random keys among those with an expire set.
  • volatile-ttl: Remove keys with the shortest time-to-live (TTL) first.
  • allkeys-lfu: Remove the least frequently used (LFU) keys.
  • volatile-lfu: Remove the least frequently used (LFU) keys among those with an expire set.

The chosen policy is configured via the maxmemory-policy directive.

Persistence Options

MS Cache offers two primary mechanisms for data persistence:

  • RDB (Snapshotting): Periodically saves a point-in-time snapshot of the entire dataset to disk. This is efficient for backups and disaster recovery.
  • AOF (Append Only File): Logs every write operation received by the server. This provides better durability as data loss is limited to the last second of operations (depending on fsync policy).

Both can be enabled and configured to suit your needs.

Important: Carefully choose your persistence strategy based on your application's durability requirements and performance considerations.

Troubleshooting

If you encounter issues with MS Cache, consider the following common problems and solutions:

  • High Latency: Check network connectivity, server load, and memory usage. Ensure your eviction policy is appropriate.
  • Data Loss: Verify that persistence (RDB/AOF) is enabled and configured correctly. Monitor disk space.
  • Connection Refused: Ensure the MS Cache server is running and listening on the correct port. Check firewall rules.
  • Out of Memory Errors: Increase the maxmemory setting or refine your eviction policy. Consider optimizing data storage.

Consult the detailed logs for more specific error messages.