[Redis] Allocation lists with jemalloc and libc

277 views
Skip to first unread message

ciainz91

unread,
Jan 6, 2015, 12:43:15 PM1/6/15
to redi...@googlegroups.com
Hello everyone! I have a question regarding the allocation of lists using the libc allocator and jemalloc. Since jemalloc is the best allocator for Redis doing some tests I would expect that would jemalloc allocated on average more keys that libc, but it did not happen ... someone can give me an explanation about that?

Josiah Carlson

unread,
Jan 6, 2015, 5:35:58 PM1/6/15
to redi...@googlegroups.com
The primary reason for Redis to use jemalloc is for memory fragmentation over time. You didn't describe your tests, but I suspect that your test consisted primarily of starting Redis, setting a memory limit, and adding keys until it stopped. If that is the case, there should be no substantial difference, as the libc and jemalloc versions will account for the same amount of allocated data, and would likely stop at just about the same time.

But if you were to implement a test that wrote and deleted keys in a somewhat realistic fashion (the same keys written and deleted between the libc and jemalloc tests), you would discover that the Redis using jemalloc used less memory from the system perspective than the one using libc, because that's exactly what it was designed and built to do.

 - Josiah

On Tue, Jan 6, 2015 at 9:43 AM, ciainz91 <ciai...@gmail.com> wrote:
Hello everyone! I have a question regarding the allocation of lists using the libc allocator and jemalloc. Since jemalloc is the best allocator for Redis doing some tests I would expect that would jemalloc allocated on average more keys that libc, but it did not happen ... someone can give me an explanation about that?

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at http://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

ciainz91

unread,
Jan 6, 2015, 6:17:52 PM1/6/15
to redi...@googlegroups.com
Thanks for the answer!
The tests we have performed are the following:
allocation of 1.5 million keys using as deletion policy AllKeys-lru first with allocator jemalloc and then Libc (key values allocated are the same for both allocators).
In the first test the keys were string while in the second were list. In the first test jemalloc has allocated more keys than Libc while in the latter the opposite.
Are the lists allocated in the same way by both allocators?

Josiah Carlson

unread,
Jan 6, 2015, 6:48:18 PM1/6/15
to redi...@googlegroups.com
Unless you have a maximum memory limit set, setting an expiration policy to allkeys-lru will do nothing. If you have set a maxmemory configuration, and you have enabled allkeys-lru eviction, then keys will be *randomly* evicted according to a sampling heuristic that intends to mostly emulate LRU - this *won't* be consistent from run to run, and *will* affect memory use and fragmentation if your keys aren't all more or less exactly the same.

Without seeing your testing code, I'm not sure that your test actually tests what you intended to test.

 - Josiah


--

ciainz91

unread,
Jan 7, 2015, 5:26:11 PM1/7/15
to redi...@googlegroups.com
I set the maxmemory to 94371840 bytes and after I started to run 15 scripts that insert 100000 keys in the database, saving after each execution the number of allocated keys. For safety I repeated the test several times and as a result I always got that with libc have been allocated more keys than jemalloc.
Since jemalloc is the best allocator for Redis I expected jemalloc would allocate more keys.
So I wanted to know if this is due to the fact that the lists are allocated differently from libc and jemalloc.

P.S.
The scripts contain a series of RPUSH followed by the name of the list and its objects.

Josiah Carlson

unread,
Jan 9, 2015, 12:42:28 PM1/9/15
to redi...@googlegroups.com
Testing *correctly* is hard, and I don't understand enough of your description of your testing procedure (without making a lot of likely wrong assumptions) to say whether or not you are testing what you think you are testing, never mind whether the difference in key count is meaningful when confronted with actual system memory use. But that is beside the point.

Redis uses jemalloc to reduce the amount of memory fragmentation that Redis suffers over time. Note: *over time*. It's not necessarily about holding more keys in memory, it's about actually using less system resources in an attempt to match desired memory use (your max memory setting) with actual memory use. In Redis, the INFO command can tell you the fragmentation ratio (the amount of memory Redis is actually using divided by the amount of memory Redis *thinks* it is using), and using jemalloc will generally keep your fragmentation ratio to 1.0-1.25 in the vast majority of use-cases (this is good). But your system's built-in malloc (unless you are using an OS that defaults to tcmalloc) will have fragmentation ratios that generally increase over time (this is bad), and seeing fragmentation ratios using system malloc of 2-5 in a week or two with a modestly loaded Redis is not uncommon.

To sum up: while in the short term your tests may show that Redis using your system malloc is able to store more keys than using jemalloc, that's not the whole story. Over time, jemalloc will generally keep Redis using lower system resources than your system malloc, which you can see if you look at the output of the INFO command.

 - Josiah


--

ciainz91

unread,
Jan 9, 2015, 4:27:35 PM1/9/15
to redi...@googlegroups.com
Ok thanks for the answers!
Have a nice weekwnd.
Reply all
Reply to author
Forward
0 new messages