You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django-d...@googlegroups.com
Hello,
Thanks to commit f02dbbe1ae[0], the Memcached cache backend using pylibmc can now keep connections open between requests. Establishing a new TCP connection is rather expensive and each saved round trips to the cache server allows to shave a few ms of response time.
It appears that in a multithreaded environment we could improve the situation even more by letting threads share the same `PyLibMCCache` instance.
Currently in Django, each thread asking for a cache backend gets its own personal Backend object[1], thus each thread also get its own connection pool to memcached. After a few requests the process ends up opening as many connections to memcached as there are threads.
If instead we allowed the connection pool to be shared between threads, connections would only be opened when necessary (other threads using all the pooled connections).
Now the important questions. Why do we have thread locals in the first place? Can we share Backend instances between threads?
After looking at the code of all cache backends I feel that nothing prevents dropping threadlocal altogether. Did I miss something?
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django-d...@googlegroups.com, Nicolas Le Manchet
I believe I'm (at least partly) responsible for this change, and the short answer is basically "historically memcache client libs have been awful with threading".
This may have improved, but in order to avoid a whole slew of potential problems (with memcache and other backbends) I copied the pattern used by database connections.
That said, I like your idea of sharing the connection pool between threads. It would mean at worst we'd wind up where we are.
--
Curtis
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Nicolas Le Manchet
unread,
Mar 9, 2018, 1:16:04 PM3/9/18
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django-d...@googlegroups.com
I've been checking this a bit more and my conclusion is that it is definitely possible to provide thread-safety for libraries that are not safe out of the box while still allowing thread-safe ones to make use of connection pools.
Now the bad news is that none of the two blessed memcached libraries (python-memcached and pylibmc) provide a good connection pool. This change would have marginal performance impact on them.