Solr 4.x Memory Usage vs. 3.x
Join the DZone community and get the full member experience.Join For Free
or, “trunk can use about 1/3 of the memory required by 3.x”
please note that these tests were created with an eye toward emphasizing the differences. for instance, i chose to sort on strings since i knew that was an area that would highlight the improvelements. even so, i’m quite impressed.
if you want to skip all the setup info, drop to the section “measurement results”.
estimating hardware requirements
at lucid, we often get asked “how much hardware do i need given x documents and y qps”? a similar question is “how many documents can i fit on a machine like z?”.
these are perfectly reasonable questions, ones i’d like to have answered myself. grant ingersoll created a spreadsheet that tries to help estimate the memory requirements here: grant’s memory estimator . looking at that, you’ll see some of the problems. there are just too many variables you have to know up front. often the hardware people are looking for some ballpark estimates before the search requirements are nailed down, which makes it even harder.
in some ways, this is like asking “how big is a ‘java’ program?”. the
answer is always “well, what do you want it to _do_?”. here at lucid,
we’ve gotten pretty good at replying “you have to prototype”. this isn’t
a cop-out, i’ve personally tried at least three times to create
guidelines for these questions for internal use. it usually takes me
about 1/2 hour to remember why i gave up my previous attempts; i quickly
get estimates like “you’ll need between 1 laptop and 3 supercomputers” –
which is totally useless. hey, i’m getting better. it’s taking me less
and less time to give up
. by the time you make ranges out of several of the variables, the range becomes humongous. variables like:
- sorting (how many fields? how many unique terms each?)
- faceting (how many fields? how many unique values each?)
- how big is your machine? solr usually comes under memory pressure first.
- what is the qps rate you expect? peak? average?
- how many fields do you intend to search? using edismax?
- what kinds of filter queries do you expect to run?
and on and on.
unfortunately, it’s often not possible to know these ahead of time, especially if solr is being in a greenfield application. if solr is replacing something else, you have some real-world data to go on when estimating query characteristics and qps since you can examine how your current search engine is being used. but even this has uncertainties. i guarantee that, as soon as you present your users with different capabilities (which you will probably do with solr) how they use the search capabilities will change. but you have to start somewhere.
you have to prototype
the only reliable guide we’ve been able to give clients is “you really have to prototype with
to see. we usually recommend you get two numbers from a “typical” piece
of hardware. note that we’re usually more concerned with query
performance than indexing performance:
- start with, say, 20m documents on your machine. use something like solrmeter or jmeter to fire queries at your hardware. keep upping the rate at which queries are sent to the server until your response time lengthens. this gives you the theoretical max sustained qps rate.
- now add, say, 5m documents to the machine and hit it with, say, 80% of the qps rate found in step 1. if your qps is sustained, add 5m more docs. repeat until your server falls over.
now you have a max qps rate and max doc count for your target hardware, you can set up various monitoring systems to alert you when you start to approach these numbers and expand capacity as appropriate. this is important to the operations people. these are the folks who are on the other end of the beeper and do not want a call at 3:00 am saying their system just stopped. they really really appreciate getting some advance warning that they’re nearing their capacity.
using the above, and seeing how various clients use solr, our first estimate is that a “reasonable” machine can handle 40-50m documents and give you a “reasonable” response rate. but already, you see part of the problem. define “reasonable” in either dimension. really, all we can offer is a place to start building out a test bed, with an order of magnitude number of documents. at least telling a client to start with 20m documents (assuming they aren’t, say, volumes of the encyclopedia britannica) starts to define the scale expectations.
still, we have some “rules of thumb”
despite all the prevarication above, we are pretty comfortable saying “start at 20m documents for prototyping”. often we see 40-50m documents on production hardware.
but in trunk everything we know is wrong
at lucid, when asked why performance is not as high as a client would like, the first thing we look at is how much memory is being used by the application. solr will typically come under pressure from memory constraints first.
the second thing we look at is garbage collection. see mark miller’s post for an excellent introduction. we’ve seen some situations in which the jvm can’t recover enough memory on a full gc cycle to finish the current operation and went into a repeated full gc cycles for many minutes. sometimes this has been tracked down to a jvm bug, sometimes a problem in solr, sometimes to simply needing more memory, etc.
so, we reflexively look at memory for “why is solr slow” and have some “gut feelings” about when and how memory pressure will manifest itself.
but in solr/lucene 4.0 (a.k.a. “trunk”) there have been some very significant improvements in how memory is used. so some of what we “know from experience” will need to be re-examined.
i ran some simple tests just to get a feel for what the effects of this work were, the results are surprising. your mileage will vary, of course. the take-away here is that capacity probably has to be re-tested on 4.0. i’d be astonished if performance were worse , but your estimates of the number of documents your current hardware can comfortably accomodate are probably low if they’re based on pre-trunk testing.
i took an 11m document dump from wikipedia and indexed it under 3x and trunk with code from late march, 2012. i created two sortable string fields, one for user and one for title, with 233k and 9.4m unique values, respectively. then i started the server fresh and looked at memory consumption after a query that sorted by both these fields. i used the jconsole and visualvm “gc now” button. the i looked at the memory consumption and object count.
here are the actual numbers:
- time to perform the first query with sorting (no warmup queries) 3x: 13 seconds, trunk: 6 seconds.
- memory consumption 3x: 1,040m trunk 366m. yes, almost a 2/3 reduction in memory use. and that’s the entire program size, not counting memory used to just start solr and jetty running.
number of objects on the heap. 3x: 19.4m trunk: 80k. no, that’s not a typo. there are
over two orders of magnitude fewer objects on the heap in trunk!
this reduction in raw memory has obvious implications for capacity since it’s usually the case that solr comes under pressure for memory reasons. in particular, once you push solr’s memory to the point where it starts swapping, you have to shard the index to get your performance back. this point is probably quite a bit further out now. i doubt that you can put 3x as many documents on a given piece of hardware, but you’ll have to test to see.
a somewhat subtler savings is at garbage collection time. i don’t care how efficient your garbage collection is, collecting 19.5m objects has got to be slower than doing the same operation on 80k objects. i suspect that memory fragmentation is reduced too, but that’s speculation.
ok, how did they do that?
well, it helped that the coding gnomes were locked in a room with neither food nor beer. actually, this represents the culmination of a bunch of different work by some very, very talented programmers who just couldn’t stand the inefficiencies they knew about once they realized they could make it better. it’s been something over a year coming i believe.
somewhere, old ‘c’ programmers are smiling (yes, i resemble that remark). the biggest savings in both memory and objects comes from allocating big buffers to hold the string data, treating it as a big block of memory with string offset and length kept in control structures. so, instead of having zillions of small string objects laying around, there are a small number of really big arrays of bytes and some control structures pointing to them.
the write-once nature of a segment helps here. consider how awkward this would be if you allowed the strings to change. this nice, tightly packed array of bytes would be subject to all the vagaries of changing data. the problem was already hard enough without re-inventing the heap manager! but since segments don’t change once written, these structures can be made when a searcher is opened without having to deal with the issues mutable data would entail. and they can be thrown away all at once when the searcher closes. these two features of solr segments make this strategy much more viable than it might otherwise be. oh, and it’s yet another reason that updating individual fields in a solr document is “something we’ll think about”…
note that for this test, both the fields i sort on are strings. i purposely chose string types because i was pretty sure it would show the improvements in memory usag clearly. i just wasn’t quite prepared for how clearly.
i’m simplifying here, and i had nothing to do with writing the code so it’s a high-level view. but the results speak for themselves. couple that with the fact that java string objects have about 40 bytes of overhead before they store the first byte of actual data and…well…
Published at DZone with permission of Erick Erickson. See the original article here.
Opinions expressed by DZone contributors are their own.