9 ms·
A quick look at the Redis source code
- jeremiep 13y agoIt's actually a quick tutorial describing how to add a new command to Redis, not an actual analysis of the source code as I expected.
- L8D 13y agoIt's more of an analysis of the structure of Redis' code.
- HeyChinaski 13y agoYeah, it's a pretty shallow introduction. I'd like to write more articles on the Redis code. The sorted set skiplist stuff is really interesting.
- ddorian43 13y agoWrite: How to write custom C commands ?
- hox 13y agoThe entire implementation of sorted sets is really interesting, with a dual implementation of ziplists and skiplists being used depending on the amount of elements in the list. I've been meaning to write bit more about Redis internals lately; maybe I'll start on that in my commute hours. I've got a couple of general articles on adding a command and adding a datatype to Redis at http://starkiller.net http://starkiller.net, but I don't get too into existing code. I'd be interested in writing a bit more about the other data structures as well as the multiple strategies used for EXPIRE (which recently changed I believe).
- rch 13y agoYou might be interested in one of these articles: http://pauladamsmith.com/articles/redis-under-the-hood.html http://pauladamsmith.com/articles/redis-under-the-hood.html http://blog.togo.io/how-to/adding-interval-sets-to-redis http://blog.togo.io/how-to/adding-interval-sets-to-redis They have both been discussed before though.
- HeyChinaski 13y agoAh yes, I remember the interval sets article. The first post is a lot more in depth than mine. Both great links, thanks.
- jeanjq 13y agoFun. This isn't really specific to Redis, but is a good introduction to the sort of thing that C programmers often get up to. You'll see this sort of function table in all sorts of C programs. Take a look at GNU stuff like make and you'll see the same format. Congratulations on the exploring.
- rdtsc 13y agoRedis is my favorite example of a very clean and beautiful example of C code, just the right amount of comments, good variable names. It is great work. You can tell Salvatore cares and has passion for what he does just by looking at his work. https://github.com/antirez/redis/tree/unstable/src https://github.com/antirez/redis/tree/unstable/src
- free652 13y agoWow it's awesome, comparing to the most of the C code that I saw it's beautiful. I like the long method names, because I can actually understand what they are doing.
- matthiasv 13y agoMaybe you haven't looked very often but nice code is out there for quite some time. Take a look at anything related to the GNOME stack (GLib, GTK+, all the applications) or the Linux kernel for example. And quite frankly, I stumble upon badly written Python way more often than C.
- spullara 13y agoThe code for those might be great, but Redis is beautiful inside and outside. I'm not a fan of the actual output of the GNOME stack. I agree the Linux kernel code is generally great to read and to use.
- 16s 13y agoIMO, OpenBSD and tarsnap are other notable samples of clean C code.
- maurycy 13y agoI'd also mention FreeBSD kernel and Niels Provos' code.
- avtar 13y ago"And quite frankly, I stumble upon badly written Python way more often than C." I'm curious, which public codebases would you consider to be examples of well written Python?
- pestrella 13y agoExcellent write up aimed at curious coders.
- qwerta 13y agoI find it fascinating to compare H2 and Derby source code. First was written by single man, has more features, is more compact and faster. Second was 'designed by committee' and evolved over long period of time. I would also post link to my project, which is sort of 'Redis in Java', but it would be probably spam.
- jahaja 13y agoNice post. I've been working quite a lot with the internals of Redis in the past few months. Adding custom commands along with the usual skimming through the builtins. Perhaps I should give people some insight by creating a few blog post as well. It's a really nice piece of software and written in clean, high quality, C. Not sure about the tests in Tcl though :).
- HeyChinaski 13y agoI was quite surprised to see the tcl tests. I'm reserving judgement until I've tried writing one though.
- jahaja 13y agoTo be fair, the tests in themselves are alright, but I'm not to familiar with tcl and have had problems with running them in a CI build with a lot of redis-servers being left behind. As the test are as far as I've seen basically integration tests it would be quite nice to have them in something like python to make them a bit more easy to handle.
- eliben 13y agoThe Redis source code is very clean and readable. It's a great example of how a non-trivial code base can be written in good C style.
- ethanazir 13y agoI would buy a book written about Redis like this.
- malkia 13y agoPostgres source code has been pleasure to read, and the commit logs are outstanding.
- GhotiFish 13y agoI'm certainly novice in C, but as I was reading, I wondered about this {"get",getCommand,2,"r",0,NULL,1,1,1,0,0}, "The fourth field, set to "r", is specifying that the command is read only and doesn’t modify any keys’ value or state. There are a whole bunch of one letter flags that you can specify in this string that are explained in detail in the nearby block comment. The field following this string should always be set to zero, and will be computed later. It’s simply a bitmask representation of the information implied by the string." Why would you opt for this, when you could specify some constants and bitwise or them together? Isn't that a more common thing to do, than to calculate a bitwise flags at run time? COMMAND_READONLY | COMMAND_RANDOM | COMMAND_NOSIDEEFFECTS ect ect ect. I'm sure there's a good reason, but this style seems strange to me. Maybe redis makes use of the string later? but I can't help but feel it should build the string based on the flags, rather than build the flags based on the string.
- HeyChinaski 13y agoIn defence of the technique, the command table is quite succinct and arguably more readable at a glance than if there were a bunch of constants |ed together. I have no idea whether this was the original motivation though.
- GhotiFish 13y agoYes. It is certainly more pithy. ACTUALLY! It reminds me of a technique Bisqwit used when he made his emulator. He used strings to define the behavior of certain instructions, the strings were actually interpreted at compile time. Though I think this is a C++ specific trick. http://www.youtube.com/watch?v=y71lli8MS8s http://www.youtube.com/watch?v=y71lli8MS8s he brings in the instruction table at 1:30
- dang 5y agoURL seems broken now but article is here: http://web.archive.org/web/20180303001631/http://www.heychinaski.com/blog/2013/10/14/a-look-at-the-redis-source-code/ http://web.archive.org/web/20180303001631/http://www.heychin...