Make CouchBase.conn thread-safe

Bug #862979 reported by Jason Gerard DeRose
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Microfiber
Fix Released
High
Jason Gerard DeRose

Bug Description

Currently Microfiber isn't thread-safe as there wasn't really a pressing need in Novacut-land and I wanted to keep things as simple as possible.

However, it's trivial to make Microfiber thread-safe as the HTTPConnection instance is the only thing that isn't thread-safe (the http connection is available on the `CouchBase.conn` attribute). Steps needed to make it thread-safe:

  1) Change `CouchBase.__init__()` to figure out (as it does now) whether an HTTPConnection or HTTPSConnection instance is needed based on the URL, but rather than creating an instance, have it just store the class on the CouchBase.Conn attribute.

  2) Add a `CouchBase._conn` instance attribute that is a threading.local() instance (this should be created in `CouchBase.__init__()`)

  3) Change `CouchBase.conn` into a property that creates the connection instance if it doesn't exist in the current thread

Now I added a "?" in the bug title because we'll do this on one condition: as long as this doesn't cause any performance regression. I doubt this will, but we're going to measure it to be sure.

With the way we're using CouchDB for event propagation, we have to be careful about performance otherwise it will introduce too much latency. Currently Microfiber is fast enough for our needs, but we can't just take that for granted because, as a cautionary tale, python-couch is *not* fast enough for our needs. I've never been able to figure out why python-couchdb is so much slower when you PUT or POST a doc to CouchDB, but it can only do about 20 docs a second (or in other words, it adds around 50ms latency).

In comparison, Microfiber can PUT/POST about 160 docs a second, meaning it only adds around 6ms latency.

Oh, also note that reusing the connection provides a substantial performance improvement. I wanted to keep microfiber as simple as possible, so at first I tried just creating a HTTPConnection inside the CouchBase._request() method for each request... this is simple, plus is thread-safe without anything fancy. However, giant performance different when you rapidly make several requests in a quick burst... which is a very typical pattern for Novacut and dmedia.

Honestly, there isn't a huge pressing need for Microfiber to be thread-safe in Novacut-land (if needed, each thread can just create its own Server or Database instance). But my hunch is having Microfiber thread-safe will probably help it get more widely adopted elsewhere.

Feedback welcomed as to whether this is even worth the trouble.

Related branches

Changed in microfiber:
milestone: 11.10 → 11.11
Changed in microfiber:
milestone: 11.11 → 11.12
Changed in microfiber:
milestone: 11.12 → 12.01
Changed in microfiber:
milestone: 12.01 → 12.02
Changed in microfiber:
milestone: 12.02 → 12.03
Changed in microfiber:
milestone: 12.03 → 12.04
Changed in microfiber:
milestone: 12.04 → 12.05
Changed in microfiber:
status: Triaged → In Progress
assignee: nobody → Jason Gerard DeRose (jderose)
Revision history for this message
Jason Gerard DeRose (jderose) wrote :

Lately there have been cases coming up in both Dmedia and Novacut where having CouchBase.conn thread-safe would be very handy, and as there seems to be no performance penalty for this, I'm pushing forward on this change.

Changed in microfiber:
status: In Progress → Fix Committed
summary: - Make `CouchBase.conn` thread-safe?
+ Make CouchBase.conn thread-safe
Changed in microfiber:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.