Friday, January 24, 2014

Why you should almost always choose Redis as your database

Whenever the topic of databases/persistence arises, I almost always recommend using Redis instead of MySQL or even any other NoSQL solution.

There are two reasons for choosing Redis almost always:
1) Redis data structures are far more intuitive and versatile means of storing data than relational databases.
To me, relational databases are a very limiting and antinatural way of structuring data. I’ve always felt that mapping the concepts of your program (whatever your style of programming, but especially if it is object-oriented or functional) to relational databases is both painful and frustrating. This is for two reasons:

- Relational databases have no concept of hierarchy – that is, no nesting. What you have is a set of arrays, instead of having a tree. There’s nothing bigger than a table, and nothing smaller than a field.

- The links between nodes are of a weak and limited type: foreign keys. So you have to bend over backwards to implement some sort of network model for your data.

(BTW, this is why ORM is a rabbit hole of the kind that nothing of real beauty can come from. No matter how good the solution is, it’s always a variant of fitting a square peg in a round hole).

Redis, although it isn’t a true tree or a graph, is far closer to either of them, because it has a rich set of data structures which are very similar to those of today’s high level programming languages. From what I’ve looked around, no other NoSQL tool offers a comparable set of data structures.

This means you’ll do far less violence to the concepts of your program when you persist its data with Redis. This makes for faster development and will considerably improve the quality of your code. More importantly, your code will be more beautiful.

2) Redis runs in RAM
Although it persists to disk, Redis data is read from and written to RAM. Since RAM is about an order of magnitude faster than a disk, this translates to queries and write operations that are roughly an order of magnitude faster. Sure, many caveats and exceptions apply, but that’s the essence of Redis’ blazing performance.

So, to sum up:
Redis will make your application 1) easier and more enjoyable to program, because it maps better to the concepts of your program; and 2) faster.
Yet…

You should not use Redis if your dataset is large (more than 2gb).
This is because it is non-trivial (though possible) to create a cluster of Redis instances, each of them holding up to 2gb (or 4, or 8). Also, if your application stores larges volumes of data, then probably Redis will never be your option because of economics (can you afford terabytes of RAM?). In that case, you should give a deep, meaningful look to Amazon S3.

(Did you notice I’m implying you should never use MySQL?)

These counterarguments to using Redis are invalid:
MySQL is the default and Redis is not production-ready: there’s much to argue against using the default technological choice for anything – and unless your clients insist of vanilla-grade software, you should seek something better than the median tool. And Redis is very, very production ready. Just look around and see who’s using it.

- Redis is not truly persistent because it runs in RAM: both of Redis’ persistence operations (journal and snapshotting) are good enough. For me, the ideal would be to have a reverse journal, where you store the negative changes (what you should apply to goback instead of starting from 0 and going forwards) – if you combine this with snapshotting, you’d have something that’s virtually lossless and fast. But going back to the main point, Redis persistence to disk is secure and reasonably fast.

Thursday, January 23, 2014

Why Redis is a Great Tool for New Applications and Startups

While it has been proven time and time again that open source databases and technology are ideal for startups and application developers due, in large, to the potentially unlimited contributors that aid in perfecting the code, when it comes time to choose a database within that open source software, what makes one stand out over the other?

Open source Redis is one of the top three databases used by new applications today. According to a survey of database users by 451 Research, Redis adoption is projected to increase from 11.3 percent to 15.9 percent in 2015. It is clear that Redis is taking off as a leading in-memory database solution, but what is it, exactly, that makes Redis so attractive to startups and application developers alike?

Redis’ popularity is due, largely, to its combination of high-performance, attractive data types, and commands that simplify application development. As new companies and applications emerge, they demand scalable high-performance databases to keep up with the exponential growth of their data.

Redis’ unique characteristics have resulted in tremendous adoption rates—making it a database of choice for many leading companies. For example, Pinterest uses Redis for the “follower graph,” which is a breakdown of who is following whom and Twitter uses Redis for its home timeline. Redis is especially well suited to new companies and applications for several key reasons.

Top performance
Redis is entirely served from RAM, which makes it faster than any other datastore (most of which are served from disk) by an order of magnitude. Furthermore, it has a simple, single-process, event-driven design, meaning it does not have to deal with lock mechanisms like other databases do, which hinder performance for many applications. The diagram below presents benchmark tests carried out for several leading databases.

Simplified application development
Developing new applications with Redis is way simpler, more intuitive and faster than other databases, including MySQL. Redis has a rich set of data structures, which are very similar to those of today’s high level programming languages that are increasingly used by application developers. The code used to build the data structures of Redis, like sets, lists, sorted lists, etc., allows users to perform really complex tasks very easily. It also offers transactions that allow users to plan multiple commands, making it thread-safe.

Conclusion
With Redis, developers do far less damage to the concepts of their programs, resulting in faster development, improved code quality and more beautiful code. This, combined with its top performance, it’s no wonder why Redis’ popularity is soaring.

My Profile

My photo
can be reached at 09916017317