0Pricing
Caching Strategies: Redis + CDN + Edge Computing · 课时

Redis 持久化与 HA

了解 Redis 的持久化选项(RDB、AOF),以及使用 Sentinel 或 Cluster 实现高可用性的策略

Redis 持久化与 HA 是 CoddyKit 上的免费 Caching Strategies: Redis + CDN + Edge Computing 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Caching Strategies: Redis + CDN + Edge Computing 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Caching Strategies: Redis + CDN + Edge Computing 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Saving Your Redis Data

Imagine your Redis server suddenly crashes! Without a plan, all the valuable data stored in its memory would be lost forever. This is where persistence comes in.

Redis persistence means saving your in-memory dataset to disk. This ensures that even if Redis restarts or crashes, your data can be restored from the saved copy.

RDB: The Snapshot Method

Redis Database (RDB) persistence works by taking periodic snapshots of your dataset. It creates a compact, point-in-time representation of your data.

  • Pros: Very fast for backups and restores, compact file size.
  • Cons: Potential for data loss between snapshots if a crash occurs.

Configuring RDB Snapshots

You can configure RDB snapshots in your redis.conf file. The save directive tells Redis when to take a snapshot. Here's an example:

save 900 1    # Save if 1 key changed in 15 mins
save 300 10   # Save if 10 keys changed in 5 mins
save 60 10000 # Save if 10000 keys changed in 1 min

AOF: The Log File Approach

Append Only File (AOF) persistence records every write operation received by the server. Instead of snapshots, it's a log of commands that can be replayed to reconstruct the dataset.

  • Pros: Much less data loss risk (can be configured for every write), human-readable log.
  • Cons: Larger file size, potentially slower restore than RDB.

Configuring AOF Sync Policy

AOF persistence is enabled with appendonly yes. You can control how often Redis syncs the AOF file to disk using appendfsync:

appendonly yes

# appendfsync always  # Sync every command (slowest, safest)
appendfsync everysec # Sync every second (good balance)
# appendfsync no      # Let OS sync (fastest, least safe)

RDB vs. AOF: Which to Choose?

Both RDB and AOF have their strengths. Often, the best approach is to use both:

  • AOF: For maximum data safety, as it logs almost every write.
  • RDB: For fast backups and disaster recovery, especially for cold starts.

If both are enabled, Redis will use the AOF file to reconstruct the dataset upon restart, as it generally guarantees less data loss.

High Availability: Always On

Beyond persistence, High Availability (HA) ensures your Redis service remains operational even if a server fails. This is crucial for critical applications that cannot tolerate downtime.

Redis offers two main strategies for HA: Sentinel and Cluster. Both involve multiple Redis instances working together to provide redundancy and automatic failover.

Redis Sentinel: The Watchdog

Redis Sentinel is a system designed to help manage Redis instances. It provides:

  • Monitoring: Constantly checks if your master and replica instances are working correctly.
  • Notification: Alerts system administrators when something goes wrong.
  • Automatic Failover: If a master instance fails, Sentinel promotes a replica to master and reconfigures others.
  • Configuration Provider: Clients connect to Sentinels to discover the current master.

Redis Cluster: Distributed & HA

Redis Cluster provides a way to run Redis across multiple nodes, offering both sharding (distributing data) and high availability.

It automatically splits your dataset among multiple Redis instances (nodes). If a master node fails, its replica is automatically promoted. This allows for horizontal scaling and ensures your data is always accessible, even with node failures.

Persistence & HA Quiz

Which of the following statements about Redis persistence and high availability is TRUE?

Recap: Data Safety & Uptime

In this lesson, we explored how to protect your Redis data and ensure continuous service. We covered:

  • RDB persistence: Snapshotting your data at intervals for fast backups.
  • AOF persistence: Logging every write operation for maximum data durability.
  • Redis Sentinel: Monitoring and automatic failover for single-instance setups.
  • Redis Cluster: Distributing data across nodes for scalable high availability.

Understanding these concepts is vital for building robust, production-ready Redis applications!

常见问题解答

「Redis 持久化与 HA」课时是免费的吗?

是的 — 「Redis 持久化与 HA」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Caching Strategies: Redis + CDN + Edge Computing 课程的其余内容,请升级到 CoddyKit PRO。 Caching Strategies: Redis + CDN + Edge Computing 课程共包含 4 节课。

「Redis 持久化与 HA」这节课中我会学到什么?

了解 Redis 的持久化选项(RDB、AOF),以及使用 Sentinel 或 Cluster 实现高可用性的策略 你通过在浏览器中直接运行的动手代码来练习 Caching Strategies: Redis + CDN + Edge Computing,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Caching Strategies: Redis + CDN + Edge Computing 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Caching Strategies: Redis + CDN + Edge Computing 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「Redis 持久化与 HA」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Caching Strategies: Redis + CDN + Edge Computing 课中编写并运行代码吗?

能。每节 Caching Strategies: Redis + CDN + Edge Computing 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Redis 持久化与 HA
  2. 使用 Redis 实现分布式缓存
  3. 使用 Redis 发布/订阅实现缓存失效
  4. Redis 集群与分片
← 返回 Caching Strategies: Redis + CDN + Edge Computing