Slashdot Mirror


Why Reactive Programming For Databases Is Awesome

Nerval's Lobster writes "'Building on many lessons learned from spreadsheets, functional languages and model-driven application development environments, Reactive Programming (RP) is a recent approach to creating applications,' Val Huber, CTO of Espresso Logic, writes in a new column. 'In the RP paradigm, you can create complex applications from a series of simple declarative statements. Many of the details of implementation and work of gluing together various sorts of application constructs can be hidden because of the declarative approach.' He goes on to argue that RP makes maintenance easier and applications more adaptable, because RP offers automatic dependency management and reuse; at the same time, he also acknowledges that RP has some shortcomings. What say you?"

25 of 165 comments (clear)

  1. I for one would love to see DBs be more like Excel by idioto · · Score: 5, Funny

    I think excel is one of the greatest programs ever and reactive programming sounds great. This article isn't very specific as to how to get started though. Is it a concept, a product or something I have to implement myself?

  2. Sounds like VHDL and similar approaches... by Lisias · · Score: 3, Interesting

    Interesting.

    Code modeled in RP sounds like VHDL, Verilog et all - the languages used to model modern Programmable Logic Arrays, Programmable Gate Arrays and Programmable Logic Devices - aka, hardware.

    No doubt this approach has its merits, but this kind or "programming" has its gotchas - a lot of gotchas.

    I stand aside for now, looking with great interest.

    --
    Lisias@Earth.SolarSystem.OrionArm.MilkyWay.Local.Virgo.Universe.org
  3. Spreadsheets? by Animats · · Score: 4, Informative

    Did this guy just reinvent spreadsheets? There's something to be said for this, but having written in Prolog, which works that way, the 'reactive programming' people have to make a better case than the article does.

    On the other hand, one of the big problems in databases is change notification. Microsoft at one point had a research project on this. The concept was that you could make a request to be notified when something of interest changed in the database. This was expressed as an SQL SELECT statement. When some change was made that would affect the output of your pending SELECT, you'd be notified. The problem was to figure out some efficient way to do this with thousands or millions of pending SELECT statements.

    Finance people use notifications like that all the time. Limits on stocks, limits on combinations of stocks, changes in key ratios, that sort of thing. But the systems for that are specialized, a special case of a more general problem. The most general form of the problem is that B wants to know when something has changed at A. Most of the solutions for this have problems. Polling is either inefficient or slow. B relying on A to message them on a change has a failure problem. Both approaches can result in overloading A with checks for events that aren't happening.

    1. Re:Spreadsheets? by Anonymous Coward · · Score: 3, Insightful

      A trigger on INSERT or UPDATE of a watched entity should kick off an INSERT into a message queue, and a trigger on INSERT in the message queue should run a script to send the message where it needs to go. Now you have a self-managing, self-logging, asynchronous system that only uses system resources when needed.

      Don't reinvent the wheel. Do learn your craft and become a master at it.

      This was solved decades ago. Next question, please.

    2. Re:Spreadsheets? by sribe · · Score: 3, Insightful

      A trigger on INSERT or UPDATE of a watched entity should kick off an INSERT into a message queue, and a trigger on INSERT in the message queue should run a script to send the message where it needs to go. Now you have a self-managing, self-logging, asynchronous system that only uses system resources when needed.

      Yes, exactly. OK, sometimes it can actually be a little tricky to figure out who needs notification and what data they need, but that's application/business logic that just needs to be worked out. (Example: client was not interested in a particular "master" record, and therefore has not received it, but the addition of a particular "detail" record means that client is now interested, so sending the changed record is not enough, the update needs to include the master and all related details. And so on and so forth, for a really rich app with rich workflow, there winds up being a surprising amount of logic required to get the right updates but no more than needed out to each client.)

      This was solved decades ago. Next question, please.

      Well... No, not really, at least not that I know of. The ability for "trigger on INSERT in the message queue should run a script to send the message where it needs to go" has actually *not* been around for decades. For way too long, this had to be a process *polling* the message queue. Triggers were limited to whatever "procedural SQL" flavor your db had, and had no access to file or network resources. Now you can write triggers in many languages, sometimes with full access to the system. Now you can from a SQL trigger send a notice, which a client can listen for via select/kqueue/libevent etc. But that's all much more recent.

    3. Re:Spreadsheets? by LordWabbit2 · · Score: 2

      Therein lies the problem, your simple trigger will fire ALL THE TIME. When all I want to watch is record A value B. Triggers are expensive, do you really want to weigh your tables down with unnecessary trigger calls? Clearly you are only dealing with a couple hundred transactions a second, but when things get into the thousands, and at peak millions, placing a trigger on a database is sheer stupidity. Triggers which are utilized are due to bad planning, last resort efforts/or because of crap code. Three guesses which one is usually the culprit? No wonder DBA's are annoyed with programmers.

      --
      There are three kinds of falsehood: the first is a 'fib,' the second is a downright lie, and the third is statistics.
  4. New buzzword? by rev0lt · · Score: 2

    From the article description I couldn't figure out a) what is actually reactive programming; b) how will that help me build better applications. Complex applications are already built from a series of simple declarative statements. In fact, we call the most simple declarative statements language available "assembly". We call the machine representation of those very complex applications built with simple declarative statements "binary". On the other hand, Brainfuck also allows you to build complex applications with very simple statements, and I'm not migrating to it anytime soon.
    Not cool, Slashdot, I'm going to have to read TFA!

    1. Re:New buzzword? by ebno-10db · · Score: 2

      Complex applications are already built from a series of simple declarative statements.

      I'm not being pedantic, as I'll be first to say that software buzzwords are not used consistently, but what you call declarative I believe they'd call imperative, as in "do this", whereas declarative is "this is the way it is (or I want it to be)". I don't know Prolog, but I believe that's considered declarative, as opposed to imperative (which describes most languages, C/C++, Java, Python, etc.).

      Haskell, which I do know, is considered at least partly declarative. You declare the relationships between variables, and the compiler figures out how to compute the stuff. For that reason, you can make the declarations in any order. For example:

      x = foo y
      y = bar 3.14

      is legal, but doesn't work the same as in imperative languages. All "variables" are immutable, and hence can only be assigned a value once (essentially initialization). In the above, y is computed first, then x is calculated using that value of y. The order of the declarations doesn't matter.

    2. Re:New buzzword? by sribe · · Score: 2

      Complex applications are already built from a series of simple declarative statements. In fact, we call the most simple declarative statements language available "assembly".

      No we don't. We call assembly, and most every language in common use, "imperative", not "declarative", because, you know, that's what they actually are. Off the top of my head, I can think of exactly 1 declarative language that is in common use: SQL. (Well, OK, I know, it's not terribly precise to call SQL just 1 language...)

    3. Re:New buzzword? by khellendros1984 · · Score: 2

      I've done small amounts of both. Haskell definitely reminded me of Prolog in some ways. I used to think of programming Prolog as building a database of facts and relationships, then executing queries against the database.

      --
      It is pitch black. You are likely to be eaten by a grue.
    4. Re: New buzzword? by DahGhostfacedFiddlah · · Score: 2

      This article's about programming paradigms, not hardware implementation. There's no difference at the processor level between assigning a variable in javascript and assembly, but that doesn't make the languages equal.

  5. Re:I for one would love to see DBs be more like Ex by rev0lt · · Score: 4, Insightful

    Usually database manuals calls them triggers.

  6. Spreadsheet programming by ilsaloving · · Score: 3, Insightful

    I can see this being useful for problems that are extremely linear and require extreme parallelization on large quantities of data, but that's about it.

    I've done this 'methodology' many times using Excel. I did it because I I needed to give the spreadsheet to other people, and wanted to avoid having Office nag about 'potentially dangerous VBA'.

    It makes it very easy to see *exactly* how your data is flowing, which is a bonus. It also uses a *lot* more ram because you are now maintaining a permanent block of memory for every single operation, for every different piece of data you are coding against. Of course, the second you get a cyclic dependency, the whole thing blows up.

    But this method of 'programming' was a natural and convenient extension based on how Excel (or any spreadsheet) operates. Nothing more.

    Is it just me, or does it seem like everyone now-a-days is trying very hard to come up with new methodologies and paradigms and web 6.5isms, so they can get their 5 minutes in the lime light?

    1. Re:Spreadsheet programming by benjymouse · · Score: 5, Informative

      I can see this being useful for problems that are extremely linear and require extreme parallelization on large quantities of data, but that's about it.

      That's the danger: Relying on your own lack of imagination to support your argument. If you cared to google a little you would realize that there are many other areas where RP could be relevant.

      Erik Meijer who pioneered the RP push at Microsoft and which lead to the Reactive Extensions for C# and JavaScript (on which the current implementations in Java are based) wrote an article with a thoughtful title: "Your mouse is a database". I could as "so is your finger".

      The idea is that a mouse is a series of "rows" with mouse positions and button states. Instead of viewing them as series of *events*, RP views them as streams of objects. That allows you to apply filters, actions, transformations etc.

      Suddenly, the events become *composable*.

      Think of a touch interface. The touch surface fires events. But if you view the events as streams of objects you can define filters and transforms which let only certain "events" through, like a stream which accepts objects starting/resetting at the "touch" but only accepts messages as long as they describe an ever longer diatance in a certain direction until the finger is lifted again. Now you have a composable stream of "flicks". You can define similar streams which will filter/generate circular moves, pinching with 2, 3 or 4 fingers.

      Think of the problem of cache eviction. You can define sweeping/eviction algorithms as (simple) streams.

      Think of asynchronous programming in a web browser. Parred with web sockets (or in .NET the awesome SignalR which uses web sockets but automatically falls back to other channels when web sockets are not available) you can build responsive, asynchronous interfaces where you *declare* what should happen as server messages are received.

      Think of a dealer system where the dealer wants to track certain papers, positions, rates. RP enables you to build a chained, composable filter where alarms are raised (alarms being messages on streams) when a rate changes more than a certain percentage within a given time period.

      I've done this 'methodology' many times using Excel.

      Sure. Haven't we all?

      It also uses a *lot* more ram because you are now maintaining a permanent block of memory for every single operation, for every different piece of data you are coding against.

      Not neccesarily . In .NET, Reactive Extensions are integrated with LINQ. As a supplement to IObservable, Reactive Extensions also define IQbservable (a *queryable* observable). Analogous to LINQ for IQueryable, LINQ applied to a IQbservable is actually capable of combine criterias and transformations "up the chain" all the way to the source. I.e. if the "event" source is capable of filtering/transforming - like a SQL database or a RSS based web feed is - LINQ for Reactive Extensions can apply the filter very early in the chain and avoid uneccesary progression of objects which would be masked later anyway.

      Is it just me, or does it seem like everyone now-a-days is trying very hard to come up with new methodologies and paradigms and web 6.5isms, so they can get their 5 minutes in the lime light?

      It is not just you. There a scores of other people out there who have always done it before 20 years ago.

      Grow up. Sometimes Computer Science does see advanced. Realizations. Erik Meijer did an awesome job with Reactive Programming. He found a *duality* between actively "pulling" objects from sequences and reactively receiving "pushed" objects from a source. Given the mathematical duality he even managed to find places where the LINQ pull model was not complete yet.

      Reactive Programming is not new. Nor was OO when it really took off. OO actually was don back in the 1970ies with Simula (IIRC). But now RP has *matured* as

      --
      Reading slashdot one-liner: (irm http://rss.slashdot.org/Slashdot/slashdot).rdf.item | fl title,desc*
  7. New? by mwvdlee · · Score: 2

    I remember experimenting with a C++ framework that would work somewhat similar. Lazy evaluation and such. http://sourceforge.net/projects/ditto, it's long since been abandoned because, well... the only thing this way of programming solves is a small part of the "R" in "CRUD". It worked brilliantly for showing information on screen and automatically updating all kinds of stuff whenever some data changed, but in the end it only solved the least difficult part of creating an application, and doing so at a relatively high CPU% cost.

    It was also supposed to have lazy/dynamic/late-binding expressions (i.e. lazy_int a = b + c;), but it turns out you only very rarely have any use for that type of expressions. At the time, there were also far superiour C++ frameworks being developed which could do these things as a side effect of supporting more useful programming models.

    This RP thing doesn't really seem to do anything more than that.

    --
    Slashdot social media options: AIM, ICQ, Yahoo, Jabber and Mobile Text. Why no MySpace?
  8. Re:Views? by beelsebob · · Score: 2

    No, the article is just terrible at explaining what FRP is. The idea of FRP is that you stop simply having the idea of having values, and start having the idea of "time varying values". So instead of having an int, which I change the state of at a bunch of times, I can have a Reactive which represents the value of the int at all possible times. You can then treat these as first class citizens, which you can manipulate, so if you have a Reactive representing the time, you can do things like time + 1, and get a new reactive int representing one second later than the current time.

    These can be combined in much more complex ways to produce much more interesting behaviours than "hey, what will the time be in one second".

    You can see a simple example of this, describing the n-bodies problem here http://noordering.wordpress.com/2009/02/01/simulating-n-bodies-and-functional-programmingre/

  9. Re:silver bullet by philip.paradis · · Score: 2

    Vampires do not fear silver bullets. They fear stakes and sunlight. Werewolves fear silver bullets.

    --
    Write failed: Broken pipe
  10. Im still trying to get accustomed by Mister+Liberty · · Score: 2

    to /active/ programming.

    Anyways, seriously, please, please please my fellow programmers. Don't jump
    on this just because it looks or sounds esoteric.

  11. The microfilm by Mister+Liberty · · Score: 2

    is in its usual place.

  12. The programmer still has to think by istartedi · · Score: 2

    OK, B+C can be evaluated once when you assign A, or it can be evaluated every time you evaluate A. Sometimes you know that A will not change after you calculate it, or that a fresh value of A is required, and sometimes you don't.

    Delaying the evaluation of A is usually safe; but it can sometimes be very slow. Like it or not, performance still matters. If you evaluated A unnecessarily inside a tight loop in a game, you'd be dead. Even less CPU intensive thinks like web apps could be killed by this kind of thing, if A is evaluated in JavaScript every time you move your mouse pointer.

    Evaluating A every time is not only slow, it might not even be correct. Let's say you pull in some weather data and then render a map. The frontal zone should be a smooth line. If you pull in new frontal boundary data while rendering the map... you'll render a broken front.

    Sorry. It looks like the programmer still has to think. Re-calculating A every time the symbol A occurs may or may not be required.

    --
    For all intensive purposes, "whom" is no longer a word. That begs the question, "who cares"?
  13. Re:I for one would love to see DBs be more like Ex by VortexCortex · · Score: 2

    Yeah, and in client side systems they're known as call-backs or event driven, etc. etc.

    I don't know about you, but if it's old let's call it new!

  14. Re:I for one would love to see DBs be more like Ex by blue+trane · · Score: 5, Informative

    In Coursera's Reactive Programming MOOC, the difference between reactive programming event-handling and traditional event-handling is described in two slides from the introductory lecture:

    http://subbot.org/coursera/reactive/callbacks.png
    http://subbot.org/coursera/reactive/howtodobetter.png

    A traditional Java event-handler is first presented, and the problems enumerated: it relies on a side-effect (the variable "count" in the example), which involves shared mutable state; events and their handlers are not first class. Reactive programming tries to do better so that complex handlers can be composed from primitive ones.

  15. Re:I for one would love to see DBs be more like Ex by LordWabbit2 · · Score: 2, Insightful

    Good god (or in this case bad god)
    Every damn company I have ever worked in has some (albeit smart) idiot who creates a smart excel/access application which does wonderful things.
    Who then distributes it to xyz people
    Then the datasource (ip address) changes and the thing breaks.
    Or the datasource changes (database table changes) and the thing breaks
    Or his hardcoded status codes changes or is expanded and the thing breaks
    Then they call you out of the blue and say, please fix it!
    Access is an evil which should die. Advocates of it are welcome to come debate the matter with me. I need fresh meat.
    Excel is extremely powerful and a handy tool, but as soon as anyone adds a datasource they should come and debate the matter with me.

    --
    There are three kinds of falsehood: the first is a 'fib,' the second is a downright lie, and the third is statistics.
  16. Re:Arg NOOOOOOO by lennier · · Score: 2

    Event-driven programming is HELL

    Isn't that right there a good reason for it being implemented once, correctly, as a core OS/language feature so that the programmer doesn't have to micromanage all the interactions, and instead just say 'this is a dependency on that; event engine, you sort it out'?

    For better or worse, ever since the mouse we've lived in an event-driven world. From what I can see, it's mostly worse, because our software tools haven't adapted to minimise the complexity, so we have the monstrous Byzantine complexity of signals, slots, callbacks, etc. RP - if it were done properly - seems like a good way of encapsulating a lot of this back into the system levels where it belongs.

    I agree that RP has to have rock-solid formal semantics for it to be dependable; that's a job for computer scientists rather than library programmers, so I hope somone in PhD land is looking seriously at this.

    --
    You are not a brain: http://books.google.com/books?id=2oV61CeDx-YC
  17. Re:ah.... by maxwell+demon · · Score: 2

    I see that you've used that word in your reply, and therefore I have to conclude that you have no idea what you were talking about. ;-)

    --
    The Tao of math: The numbers you can count are not the real numbers.