News for hackers. Stuff that matters.
Re:A multithreaded fork of Redis that is faster by tengbretson on Monday October 07, 2019 @01:05PM (#21183216)
I've always considered the single-threaded nature of Redis to be one of its greatest features.
Re:A multithreaded fork of Redis that is faster by femto113 on Monday October 07, 2019 @01:14PM (#21183334)
Only one thread can access the data at any given time, so it seems like most of the things you'd expect to be guaranteed by a single thread still are. I found this comment particularly interesting

   Unlike most databases the core data structure is the
   fastest part of the system. Most of the query time
   comes from parsing the REPL protocol and copying data
   to/from the network.
I wonder if anyone in the Redis ecosphere has explored a binary client server protocol, something that could be parsed/compiled on the client and then executed without parsing on the server, if the above is really true seems like that might offer even more perf gain than multithreading on the server.
Re:A multithreaded fork of Redis that is faster by smueller1234 on Monday October 07, 2019 @01:47PM (#21183778)
From having played with/worked on profiling and optimizing Redis in the 2.6 timeframe, I can confirm that at least for small/simple operations, this is true, the data structure access is a small fraction of the cost.

One related choice that Redis makes (or made at the time) is to rely extremely heavily on the malloc implementation, rather than doing work to manage it's memory internally. Even a very trivial, naive free list provided a modest speed-up, for example.

There are a lot of these choices in the code base, largely owing to maintainability concerns (though antirez can surely speak for himself). Given how easy it is for an otherwise uninitiated C programmer such as myself to hack on it, I struggle to disagree with the prioritization. :)

Re:A multithreaded fork of Redis that is faster by tyingq on Monday October 07, 2019 @02:14PM (#21184143)
The excerpted comment in a format mobile readers can see without left/right scrolling:

"Unlike most databases the core data structure is the fastest part of the system. Most of the query time comes from parsing the REPL protocol and copying data to/from the network."

Re:A multithreaded fork of Redis that is faster by cheald on Monday October 07, 2019 @01:33PM (#21183589)
The human-readable/writable protocol is one of my favorite things about Redis, tbh.

I can see cases where a really optimized system could benefit from a binary protocol, but I suspect it'd be a loss for most people.

Re:A multithreaded fork of Redis that is faster by axaxs on Monday October 07, 2019 @02:14PM (#21184136)
Why not just offer both?
Re:A multithreaded fork of Redis that is faster by femto113 on Monday October 07, 2019 @03:29PM (#21184978)
That was my thinking as well, though taking a peek at the actual code suggests that there's a pretty deep expectation that the client is speaking strings, e.g. in code that handles the ZRANGE command[1] I see

    if (c->argc == 5 && !strcasecmp(c->argv[4]->ptr,"withscores"))
and a quick grep suggests that's a common pattern

    % grep argv src/*.c | grep -c -e 'str\(case\)*cmp'
    482
I guess this means someone would have to tackle creating an intermediate binary format first, rewriting the command handlers to expect that format, and then making client libraries that can produce the format. Perhaps still worth it in the end, but not trivial.

[1] https://github.com/antirez/redis/blob/unstable/src/t_zset.c#...

Re:A multithreaded fork of Redis that is faster by lmm on Monday October 07, 2019 @04:48PM (#21185798)
Is this really "unlike most databases"? I remember MySQL posting profiling data years ago showing that for looking up by primary-key, 3/4 of the time was spent parsing SQL. (They went on to introduce support for querying with the Memcached protocol to address this)
Re:A multithreaded fork of Redis that is faster by ijpoijpoihpiuoh on Monday October 07, 2019 @04:51PM (#21185820)
That's really surprising if true, considering the SQL should only need to be parsed once.

    SELECT foo FROM Table WHERE key = @mykey;
Then you bind the parameter to whatever you're interested in.
Re:A multithreaded fork of Redis that is faster by lmm on Tuesday October 08, 2019 @08:18AM (#21191383)
Prepared statements are per-connection and a lot of time you want to use connections from a single pool that's used for all you different queries, so you can't really use them.
Re:A multithreaded fork of Redis that is faster by ijpoijpoihpiuoh on Tuesday October 08, 2019 @09:02AM (#21191775)
Even with that, the SQL would be parsed once per connection? So, the costs should be de minimis, unless the benchmark were short indeed?
Re:A multithreaded fork of Redis that is faster by lmm on Tuesday October 08, 2019 @09:55AM (#21192319)
> Even with that, the SQL would be parsed once per connection?

In a webserver-like context it's once per query one way or another - the server process is stateless-ish between page loads, so each page load is either a from-scratch connection or a connection taken from a pool, but even if you're pooling you can't use prepared statements in practice (you can't leave a prepared statement on a connection that you return to the pool because you'll eventually exhaust the database server's memory that way, and you'd have to resubmit the prepared statement every time you took a connection out of the pool anyway because there's no way to know whether this connection has run this page already or not).

If you assume a page that's just displaying one database row, which is not the only use case but a common one, then each page load is one query and that query will have to be parsed for each page load, short of doing something like building a global set of all your application's queries and having your connection-pool logic initialise them for each connection.

Re:A multithreaded fork of Redis that is faster by ijpoijpoihpiuoh on Tuesday October 08, 2019 @10:47AM (#21192886)
In a database product I'm familiar with, the prepared statements are cached according to their content and those cached objects are shared between connections. Only if they fall out of the cache do they have to be re-parsed. I had assumed that's how all databases worked.

I'm somewhat surprised at the mechanism you're describing, but now I read the documentation it does seem to be the case. I wonder if a small piece of middle-ware might be sufficient to replicate the behavior I'm describing on a connection pool, and whether that would be desirable.

Re:A multithreaded fork of Redis that is faster by graycat on Monday October 07, 2019 @04:36PM (#21185678)
See my post for essentially a "binary" interface: For my key-value store (I wrote because I thought that writing the code would be faster than understanding Redis, :-)), a client uses just standard TCP/IP sockets to send a byte array. The array has the serialization of an instance of a class. Then my key-value store receives the byte array and deserializes to get a copy of the client's instance of the class. So, with the byte array, maybe can count the interface as "binary"? I'm unsure of the speed of de/serialization.
Re:A multithreaded fork of Redis that is faster by moab on Monday October 07, 2019 @01:12PM (#21183297)
Can you elaborate on why? As someone who works on parallel/concurrent algorithms and data structures a single-threaded system design seems anathema to most research over the past decade (maybe excluding H-Store, which uses local single-threadedness in an interesting way).
Re:A multithreaded fork of Redis that is faster by twic on Monday October 07, 2019 @01:36PM (#21183642)
This reminds me of LMAX, who found the fastest way to build their stock exchange was to make the core logic single-threaded, surrounded by multithreaded I/O:

https://www.martinfowler.com/articles/lmax.html

I believe other (grown-up/legacy!) exchanges work the same way.

I wonder how much of the direction of concurrency research is driven by the fact that there is much more publishable work to be done in managing concurrency rather than avoiding it!

Re:A multithreaded fork of Redis that is faster by lllr_finger on Monday October 07, 2019 @06:08PM (#21186503)
Disruptor benefits from lock free access using a ring buffer. You can get amazing results with a single thread but that aspect isn't central to the pattern.
Re:A multithreaded fork of Redis that is faster by nvarsj on Monday October 07, 2019 @05:36PM (#21186211)
The disruptor isn’t single threaded at all. Maybe you mean it can pin a core to a consumer that just busy waits on the queue. That is a common technique used in very low latency systems when not running on a real time OS.
Re:A multithreaded fork of Redis that is faster by twic on Tuesday October 08, 2019 @05:26AM (#21190278)
The matching engine is single threaded. It uses disruptors to communicate with IO threads.
Re:A multithreaded fork of Redis that is faster by nicolaslem on Monday October 07, 2019 @01:30PM (#21183568)
Not parent but the single threaded approach really shines when executing Lua scripts in Redis. Except for some very specific edge cases, the execution of a script is atomic and since no command is ever processed concurrently you can rule out data races.

I am sure it would be possible to provide these guaranties while offering concurrent execution but most likely at the expense of a simple design.

Re:A multithreaded fork of Redis that is faster by jdsully on Monday October 07, 2019 @01:33PM (#21183594)
KeyDB provides that guarantee via a lock. However that does mean long running scripts don’t benefit from the multithreading. Ditto for modules.

The goal is 100% Redis compatibility so I can’t compromise on atomicity.

Re:A multithreaded fork of Redis that is faster by avar on Monday October 07, 2019 @02:21PM (#21184232)
It shines right up until the moment someone writes a Lua command that takes more than a trivial amount of time, now your entire server is blocked in a pile-up of connections waiting on some relatively trivial amount of code being executed.
Re:A multithreaded fork of Redis that is faster by aflag on Monday October 07, 2019 @03:32PM (#21185011)
Couldn't you just rollback the change or fix the lua script?
Re:A multithreaded fork of Redis that is faster by thamer on Monday October 07, 2019 @06:03PM (#21186459)
Or just embrace modern hardware and use threads. This is the experience many people have running Redis in production with 21st-century hardware: https://twitter.com/rbranson/status/540565059337195520
Re:A multithreaded fork of Redis that is faster by aflag on Tuesday October 08, 2019 @08:29AM (#21191482)
That's a different issue. If they're in that situation without any custom scripts and the latency is cropping up, they should consider using a different tool or changing their codebase.
Re:A multithreaded fork of Redis that is faster by nitwit005 on Monday October 07, 2019 @02:07PM (#21184063)
You can run multiple instances of redis on the same machine, which gets you parallelism from multiple processes instead of multiple threads. There's some extra deployment effort in doing so, but it's not generally a big issue.

It also isn't exactly single threaded. It calls fork when it wants to persist data to disk, which is functionally similar to starting up a thread to do disk IO.

Re:A multithreaded fork of Redis that is faster by sciurus on Monday October 07, 2019 @02:52PM (#21184562)
Going from a single processes holding all your data to having your data sharded across multiple processes can be more than a little effort.

Also, it might now have any benefit for you. Imagine a certain key is particularly hot. Having one multithreaded redis process handling access to it might speed things up. Running multiple sharded redis processes won't, since only one of them will have that key.

Re:A multithreaded fork of Redis that is faster by nitwit005 on Monday October 07, 2019 @06:04PM (#21186474)
Most redis users are running multiple instances regardless. That some of them happen to be on the same machine is largely a detail.

It would make a little difference if there was a single hot key, but that's a bit unusual. Typically there's some subset of keys that are hot, and you can get them to hash across instances. People also tend to cache those values on the clients, as banging on redis constantly is a waste.

Re:A multithreaded fork of Redis that is faster by derefr on Monday October 07, 2019 @07:56PM (#21187280)
> but that's a bit unusual

It's pretty common when using a single Redis stream as a lightweight Kafka.

Re:A multithreaded fork of Redis that is faster by BubRoss on Monday October 07, 2019 @03:07PM (#21184724)
Doesn't that end up duplicating lots of data and work since the processes are independent? They don't share data do they?
Re:A multithreaded fork of Redis that is faster by tomjakubowski on Monday October 07, 2019 @03:17PM (#21184842)
fork() on Linux (and many other OSes) shares pages between the parent/child processes in a copy-on-write scheme
Re:A multithreaded fork of Redis that is faster by braindeath on Monday October 07, 2019 @05:52PM (#21186358)
That doesn’t mean multiple long running processes will share memory though unless they actually create a shared mapping, which I think the gp is referring to. In addition to tremendous memory waste this also kills cache performance.
Re:A multithreaded fork of Redis that is faster by tomjakubowski on Monday October 07, 2019 @06:43PM (#21186790)
I'd presume the persistence / I/O process would be pretty short lived: its job is to save a snapshot to disk so there's not much reason to let it live long.
Re:A multithreaded fork of Redis that is faster by BubRoss on Monday October 07, 2019 @09:32PM (#21187818)
How does that share data between multiple Redis processes?
Re:A multithreaded fork of Redis that is faster by atonse on Monday October 07, 2019 @01:25PM (#21183487)
Is it because of the consistency guarantees you can get from single threaded operation?

Because it seems like this guarantees that. It mostly parallelizes the command parsing and networking side. The actual core hash table is guarded by a global lock, so you could still get all those single-threaded guarantees.

Re:A multithreaded fork of Redis that is faster by JackFr on Monday October 07, 2019 @03:22PM (#21184908)
Won't it potentially allow out of order updates? Or am I missing something.
Re:A multithreaded fork of Redis that is faster by acroback on Monday October 07, 2019 @02:42PM (#21184469)
Oh... Tell me more why being not multithreaded is a failure for Redis?