Benchmarking Message Queue Latency
Benchmarking systems is difficult because test systems aren't real systems, test load isn't real load, and latency doesn't scale linearly. With good tools, like the ones shown in this article, it's possible to characterize behavior under a wider variety of conditions and maybe even predict behavior, including inflection points, under real load.
Join the DZone community and get the full member experience.Join For Free
about a year and a half ago, i published dissecting message queues , which broke down a few different messaging systems and did some performance benchmarking. it was a naive attempt and had a lot of problems , but it was also my first time doing any kind of system benchmarking. it turns out benchmarking systems correctly is actually pretty difficult and many folks get it wrong. i don’t claim to have gotten it right, but over the past year and a half, i’ve learned a lot, tried to build some better tools, and improve my methodology.
tooling and methodology
the dissecting message queues benchmarks used a framework i wrote which published a specified number of messages effectively as fast as possible, received them, and recorded the end-to-end latency. there are several problems with this. first, load generation and consumption run on the same machine. second, the system under test runs on the same machine as the benchmark client—both of these confound measurements. third, running “pedal to the metal” and looking at the resulting latency isn’t a very useful benchmark because it’s not representative of a production environment (as gil tene likes to say, this is like driving your car as fast as possible, crashing it into a pole, and looking at the shape of the bumper afterwards—it’s always going to look bad). lastly, the benchmark recorded average latency, which, for all intents and purposes, is a useless metric to look at .
i wrote flotilla to automate “scaled-up” benchmarking—running the broker and benchmark clients on separate, distributed vms. flotilla also attempted to capture a better view of latency by looking at the latency distribution, though it only went up to the 99th percentile, which can sweep a lot of really bad things under the rug as we’ll see later. however, it still ran tests at full throttle, which isn’t great.
bench is an attempt to get back to basics. it’s a simple, generic benchmarking library for measuring latency. it provides a straightforward requester interface which can be implemented for various systems under test. bench works by attempting to issue a fixed rate of requests per second and measuring the latency of each request issued synchronously. latencies are captured using hdr histogram , which observes the complete latency distribution and allows us to look, for example, at “six nines” latency.
introducing a request schedule allows us to measure latency for different configurations of request rate and message size, but in a “closed-loop” test, it creates another problem called coordinated omission . the problem with a lot of benchmarks is that they end up measuring service time rather than response time , but the latter is likely what you care about because it’s what your users experience.
the best way to describe service time vs. response time is to think of a cash register. the cashier might be able to ring up a customer in under 30 seconds 99% of the time, but 1% of the time it takes three minutes . the time it takes to ring up a customer is the service time, while the response time consists of the service time plus the time the customer waited in line. thus, the response time is dependent upon the variation in both service time and the rate of arrival. when we measure latency, we really want to measure response time.
now, let’s think about how most latency benchmarks work. they usually do this:
- note timestamp before request, t 0 .
- make synchronous request.
- note timestamp after request, t 1 .
- record latency t 1 – t 0 .
- repeat as needed for request schedule.
what’s the problem with this? nothing, as long as our requests fit within the specified request schedule. for example, if we’re issuing 100 requests per second and each request takes 10 ms to complete, we’re good. however, if one request takes 100 ms to complete, that means we issued only one request during those 100 ms when, according to our schedule, we should have issued 10 requests in that window. nine other requests
have been issued, but the benchmark effectively coordinated with the system under test by backing off. in reality, those nine requests waited in line—one for 100 ms, one for 90 ms, one for 80 ms, etc. most benchmarks don’t capture this time spent waiting in line, yet it can have a
effect on the results. the graph below shows the same benchmark with coordinated omission both uncorrected (red) and corrected (blue):
hdr histogram attempts to correct coordinated omission by filling in additional samples when a request falls outside of its expected interval. we can also deal with coordinated omission by simply avoiding it altogether—always issue requests according to the schedule.
message queue benchmarks
i benchmarked several messaging systems using bench—rabbitmq (3.6.0), kafka (0.8.2.2 and 0.9.0.0), redis (2.8.4) pub/sub, and nats (0.7.3). in this context, a “request” consists of publishing a message to the server and waiting for a response (i.e. a round trip). we attempt to issue requests at a fixed rate and correct for a coordinated omission, then plot the complete latency distribution all the way up to the 99.9999th percentile. we repeat this for several configurations of request rate and request size. it’s also important to note that each message going to and coming back from the server are of the specified size, i.e. the “response” is the same size as the “request.”
the configurations used are listed below. each configuration is run for a sustained 30 seconds.
- 256b requests at 3,000 requests/sec (768 kb/s)
- 1kb requests at 3,000 requests/sec (3 mb/s)
- 5kb requests at 2,000 requests/sec (10 mb/s)
- 1kb requests at 20,000 requests/sec (20.48 mb/s)
- 1mb requests at 100 requests/sec (100 mb/s)
these message sizes are mostly arbitrary, and there might be a better way to go about this. though i think it’s worth pointing out that the ethernet mtu is 1500 bytes, so accounting for headers, the maximum amount of data you’ll get in a single tcp packet will likely be between 1400 and 1500 bytes.
redis and nats
redis pub/sub and nats have similar performance characteristics. both offer very lightweight, non-transactional messaging with no persistence options (discounting redis’ rdb and aof persistence, which don’t apply to pub/sub), and both support some level of topic pattern matching. i’m hesitant to call either a “message queue” in the traditional sense, so i usually just refer to them as message brokers or buses. because of their ephemeral nature, both are a nice choice for low-latency, lossy messaging.
redis tail latency peaks around 1.5 ms.
nats performance looks comparable to redis. latency peaks around 1.2 ms.
the resemblance becomes more apparent when we overlay the two distributions for the 1kb and 5kb runs. nats tends to be about 0.1 to 0.4 ms faster.
the 1kb, 20,000 requests/sec run uses 25 concurrent connections. with concurrent load, tail latencies jump up, peaking around 90 and 120 ms at the 99.9999th percentile in nats and redis, respectively.
large messages (1mb) don’t hold up nearly as well, exhibiting large tail latencies starting around the 95th and 97th percentiles in nats and redis, respectively. 1mb is the default maximum message size in nats. the latency peaks around 214 ms. again, keep in mind these are synchronous, roundtrip latencies.
apcera’s ivan kozlovic pointed out that the version of the nats client i was using didn’t include a recent performance optimization. before, the protocol parser scanned over each byte in the payload, but the newer version skips to the end (the previous benchmarks were updated to use the newer version). the optimization does have a noticeable effect, illustrated below. there was about a 30% speedup with the 5kb latencies.
the difference is even more pronounced in the 1mb case, which has roughly a 90% speedup up to the 90th percentile. the linear scale in the graph below hides this fact, but at the 90th percentile, for example, the pre-optimization latency is 10 ms and the optimized latency is 3.8 ms. clearly, the large tail is mostly unaffected, however.
in general, this shows that nats and redis are better suited to smaller messages (well below 1mb), in which latency tends to be sub-millisecond up to four nines.
rabbitmq and kafka
rabbitmq is a popular amqp implementation. unlike nats, it’s a more traditional message queue in the sense that it supports binding queues and transactional delivery semantics. consequently, rabbitmq is a more “heavyweight” queuing solution and tends to pay an additional premium with latency. in this benchmark, non-durable queues were used. as a result, we should see reduced latencies since we aren’t going to disk.
latency tends to be sub-millisecond up to the 99.7th percentile, but we can see that it doesn’t hold up to nats beyond that point for the 1kb and 5kb payloads.
kafka , on the other hand, requires disk persistence, but this doesn’t have a dramatic effect on latency until we look at the 94th percentile and beyond, when compared to rabbitmq. writes should be to page cache with flushes to disk happening asynchronously. the graphs below are for 0.8.2.2.
once again, the 1kb, 20,000 requests/sec run is distributed across 25 concurrent connections. with rabbitmq, we see the dramatic increase in tail latencies as we did with redis and nats. the rabbitmq latencies in the concurrent case stay in line with the previous latencies up to about the 99th percentile. interestingly, kafka doesn’t appear to be significantly affected. the latencies of 20,000 requests/sec at 1kb per request are not terribly different than the latencies of 3,000 requests/sec at 1kb per request, both peaking around 250 ms.
what’s particularly interesting is the behavior of 1mb messages vs. the rest. with rabbitmq, there’s almost a 14x difference in max latencies between the 5kb and 1mb runs with 1mb being the faster. with kafka 0.8.2.2, the difference is over 126x in the same direction. we can plot the 1mb latencies for rabbitmq and kafka since it’s difficult to discern them with a linear scale.
i tried to understand what was causing this behavior. i’ve yet to find a reasonable explanation for rabbitmq. intuition tells me it’s a result of buffering—either at the os level or elsewhere—and the large messages cause more frequent flushing. remember that these benchmarks were with transient publishes. there should be no disk accesses occurring though my knowledge of rabbit’s internals is admittedly limited. the fact that this behavior occurs in rabbitmq and not redis or nats seems odd. nagle’s algorithm is disabled in all of the benchmarks (tcp_nodelay). after inspecting packets with wireshark, it doesn’t appear to be a problem with delayed acks.
to show just how staggering the difference is, we can plot kafka 0.8.2.2 and rabbitmq 1mb latencies alongside redis and nats 5kb latencies. they are all within the same ballpark. whatever the case may be, both rabbitmq and kafka appear to handle large messages extremely well in contrast to redis and nats.
this leads me to believe you’ll see better overall throughput, in terms of raw data, with rabbitmq and kafka, but more predictable, tighter tail latencies with redis and nats. where slas are important, it’s hard to beat nats. of course, it’s unfair to compare kafka with something like nats or redis or even rabbitmq since they are very different (and sometimes complementary), but it’s also worth pointing out that the former is much more operationally complex.
however, benchmarking kafka 0.9.0.0 (blue and red) shows an astounding difference in tail latencies compared to 0.8.2.2 (orange and green).
kafka 0.9’s performance is much more in line with rabbitmq’s at high percentiles as seen below.
likewise, it’s a much closer comparison to nats when looking at the 1kb and 5kb runs.
as with 0.8, kafka 0.9 does an impressive job dealing with 1mb messages in comparison to nats, especially when looking at the 92nd percentile and beyond. it’s hard to decipher in the graph below, but kafka 0.9’s 99th, 99.9th, and 99.99th percentile latencies are 0.66, 0.78, and 1.35 ms, respectively.
kafka always immediately writes all data to the filesystem and supports the ability to configure the flush policy that controls when data is forced out of the os cache and onto the disk using the flush. this flush policy can be controlled to force data to disk after a period of time or after a certain number of messages has been written.
however, there don’t appear to be any changes in the default flushing configuration between 0.8 and 0.9. the default configuration disables application fsync entirely, instead relying on the os’s background flush. jay kreps indicates it’s a result of several “high percentile latency issues” that were fixed in 0.9. after scanning the 0.9 release notes , i was unable to determine specifically what those fixes might be. either way, the difference is certainly not something to scoff at.
as always, interpret these benchmark results with a critical eye and perform your own tests if you’re evaluating these systems. this was more an exercise in benchmark methodology and tooling than an actual system analysis (and, as always, there’s still a lot of room for improvement). if anything, i think these results show how much we can miss by not looking beyond the 99th percentile. in almost all cases, everything looks pretty good up to that point, but after that things can get really bad . this is important to be conscious of when discussing slas.
i think the key takeaway is to consider your expected load in production, benchmark configurations around that, determine your allowable service levels, and iterate or provision more resources until you’re within those limits. the other important takeaway with respect to benchmarking is to look at the complete latency distribution. otherwise, you’re not getting a clear picture of how your system actually behaves.
Published at DZone with permission of Tyler Treat, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.