Skip to content

Add multi_set() - #531

Open
RayBB wants to merge 4 commits into
aio-libs:masterfrom
RayBB:feature/multi-set
Open

RayBB wants to merge 4 commits into
aio-libs:masterfrom
RayBB:feature/multi-set

Conversation

@RayBB

@RayBB RayBB commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Fixes #436.

Adds multi_set(mapping, exptime=0), the counterpart of multi_get(). It pipelines a set for each item on one connection instead of a round trip per key. It returns None and raises on failure, like set() does for an error reply. A list of failed keys (pymemcache, Django) would be the only sign that a too-large item was dropped. Per-key bools would always be True, because memcached never answers set with NOT_STORED.

  • Keys, exptime and the flag handler are checked before a connection is taken. An empty mapping doesn't touch the pool.
  • If memcached rejects only some items (SERVER_ERROR object too large for cache), every reply is still read. The connection is kept, and ClientException("Memcached multi_set failed for some keys", {key: reply}) is raised. All the other items were stored.
  • Any other unexpected reply, or EOF, raises ClientException("Memcached multi_set failed", reply) at once and discards the connection.
  • Commands are written in batches of up to 1000 items or about 1 MB, and the replies to the previous batch are read while the next is sent. Writing never waits for drain(), so it can't block on memcached, which stops reading while its replies back up. Batching keeps memory bounded, along with the time asyncio spends per write, which grows with the number of chunks buffered. 1001 × 1 MB takes about as long as a loop of set(), and 100k small keys take about 0.2s.

multi_get() has no counterpart for writing, so setting many keys costs a
round trip each (aio-libs#436). multi_set() pipelines a set command per item on
one connection.

Arguments are validated, and the flag handler run, before a connection
is taken. If memcached rejects only some items, as it does for one too
large, every reply is still read and the connection is kept, so the
ClientException can name each rejected key and the caller knows the
rest were stored. Any other unexpected reply raises at once and
discards the connection, as the stream can't be trusted.

Commands go out in batches of up to 1000 items or about 1 MB, and the
replies to each batch are read while the next is sent. Writing never
waits for the transport to drain, so it can't block on memcached, which
stops reading while its replies back up. Batching bounds memory (on
Python 3.11 writelines() copies what it is given) and the time asyncio
spends per write, which grows with the number of chunks buffered.
@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.55%. Comparing base (4fae7e8) to head (f06a916).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #531      +/-   ##
==========================================
+ Coverage   98.37%   98.55%   +0.18%     
==========================================
  Files          10       11       +1     
  Lines        1229     1386     +157     
  Branches       65       81      +16     
==========================================
+ Hits         1209     1366     +157     
  Misses         12       12              
  Partials        8        8              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@greptile-apps

greptile-apps Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5 Tier: apex

[Medium impact] The PR appears safe to merge, with no new actionable issues found.

Reviews (3) · Last reviewed commit: "Merge master" · Reviewed by Greptile

Comment thread aiomcache/client.py Resolved
Comment thread aiomcache/client.py Resolved
The flag handler runs for every item before a connection is taken, so
the timeout doesn't cover it, and its results are kept until the call
ends.
Comment thread aiomcache/client.py Resolved
Comment thread aiomcache/client.py
Comment on lines +435 to +437
keys.append(key)
header = b"set %b %a %a %a\r\n" % (key, flags, exptime, len(value))
chunks.extend((header, value, b"\r\n"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multi_get() is just one command, right? I had assumed from the feature request that memcached had another command for doing multiple sets in the same fashion.

If the only thing this is gaining is pipelining a sequence of sets I'm a little more skeptical on its utility (I'd expect the performance to not be that much better than just doing asyncio.gather() on a sequence of set() requests). If we really want this, maybe there's a way to make pipelining a general purpose solution that can be used with any method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dreamsorcerer Right, there's no multi-set command; get takes several keys but set doesn't, so it's only pipelining. It makes a big difference though, as gather() pays a round trip per item across pool_size connections. Against local memcached (default pool_size=2):

Items gather(set) multi_set
100 × 100 B 54 ms 1.8 ms
1,000 × 100 B 209 ms 3.7 ms
10,000 × 100 B 1,367 ms 33 ms
1,000 × 10 KB 114 ms 28 ms

With pool_size=10 it's still 3–21× faster, and over a real network the gap would be wider. A general pipeline API would be nice, but it's a bigger change: each command's reply parsing would need splitting from sending it. I'm happy to try that if you'd prefer it over multi_set(), or as a follow-up.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement multi_set method

2 participants