跳到正文
Modal Blog· Daniel Shaar·· 2025-05-20AI 评分23

Modal 无服务器 KV 存储 Dict 存储上限提升至无限

Modal's serverless KV store gets its limit raised to infinity

AI 导读

Modal 宣布其无服务器键值存储 Dict 迎来重大升级:存储上限从 10GiB 提升至无限,条目过期策略改为自上次写入或读取起 7 天,并新增 skip_if_exists 锁定标志与数据持久化支持。新特性适用于所有新建 Dict,可实现类 LRU 缓存和分布式锁定,确保高并发下昂贵函数调用仅执行一次。

正文

Modal’s Dict primitive provides users with a simple TTL’ed (time-to-live) key-value store that can be accessed from any container within the same workspace environment. Dicts are well-suited for things like caching the results of function calls and communicating state changes among a fleet of containers.

Today, we’re excited to announce some major improvements to Dicts, including smarter caching, a new locking feature, and data durability!

🧑‍🚀 A few small changes to Dict, a giant leap for Dict use

Here’s what we’ve changed:

Legacy DictsNew Dicts
Storage limit10GiBUnlimited
Item expiry policy30 days since last write7 days since last write OR read
Locking primitiveN/A.put() now supports a skip_if_exists flag
Durability❌✅

These changes will apply to all newly created Dicts. Some cool things we think these features enable:

  • LRU-like caching: now that reading extends an item’s TTL, hot cache entries will stick around for as long as they’re needed. And with unlimited items, there’s no need to worry about evicting useful data.
  • Distributed locking: in the event that many containers try to perform a redundant operation or state change, you can guarantee “exactly once” semantics using skip_if_exists.

With these new properties, let’s see how we can better tackle a common use case for Dicts: reducing backend load by caching function call results.

🧱 Building a request cache, Dict by Dict

Let’s look at a common app structure without Dicts that we may want to build and optimize on Modal. In this example, we have an “expensive” function that takes a while to run, along with a high concurrency Web Function as a gateway:

After running this app in production for a while, we discover that users are issuing the same few requests to the Web Function—who knew that figuring out 13 squared is 169 is all the rage. Not only that, but our app typically sees bursts of traffic for these hot requests.

As was commonly done by Modal users with the previous version of Dicts, we can define some sort of request caching class to wrap our function calls. A sample interface could look like:

We go ahead and implement some straightforward caching logic—check the cache, if the entry isn’t there, then make the call ourselves and add it to the cache when we’re done. Problem solved!

Or so we thought… turns out those bursts of traffic contain many requests that come in all at once, so we still end up making a bunch of expensive requests to our backend code before the cache is populated. We could work hard to narrow down that race condition window and handle the edge cases. But really, wouldn’t it be great if we could guarantee that we only make the one call to our expensive function?

With Dicts, we can make something quite snazzy to do just this!

🔒 Deduping requests—pop it, lock it

Diagram of request cacher

Glossing over how a production version of this (that we hope to release in our client 👀) would handle various failure modes, the request handling logic now looks like:

  • Try to “acquire a lock” by putting a pending entry in the Dict if it doesn’t already exist.
  • If someone else is / was working on the request, we read the Dict entry and poll for / fetch the result.
  • Otherwise, assuming we successfully wrote the pending entry:
    • We .spawn() a function call and insert its handle as an in_progress Dict entry.
    • Once the function call is complete, we insert the result as a completed Dict entry.

Here’s a sample implementation:

The proof is in the dashboard, so here I’ve issued 3 identical requests to our Web Function. The second request was deduped against the first, and the third request just got the result from cache:

Screenshot of not-so-expensive-function-endpoint dashboard

This not only speeds up our customer experience significantly, but we also ended up calling the expensive function only once—success!

Screenshot of expensive-function dashboard

💸 Shut up and give me my Dicts

Whether it’s caching, locking, or some other state management, just create a new Dict to get started! For more details, check out our docs. Caveat: to use the skip_if_exists flag, you may need to upgrade your client version.

Got questions? Come hang out in our community Slack—we’d love to hear what you’re building.

来源:Modal Blog · modal.com