Space4J is a simple database system that will let you work with Java Collections in memory.
Instead of having to perform a SQL SELECT to fetch a User from a database table, you can just
access the users map (java.util.Map) and call users.get(id). With Space4J, all your data
is kept in memory inside the JVM. There is no need for an extra database application, a socket connection, a
JDBC driver, a SQL statement or any kind of ORM tool. Your data is just there, in memory, inside the JVM,
inside objects, inside Java maps. What can be faster and more straightforward than that?
How will the objects in memory be saved to disk, so you don't lose them when your system crashes? Whenever
you want to perform an operation that will modify (= insert, edit or delete) your objects or maps, you
will create a Command object. This Command object will modify your data and then it will be
serialized and saved to disk in a log file. When you restart your system or application, the past commands
are read from the log files and re-applied, generating the exact same data set you had before the crash.
Won't my log files become huge, taking forever to re-apply all commands? No. From time to time your system or
application can take a snapshot of all your data to disk. Space4J keeps all your data inside the Space
object. When taking a snapshot, the whole Space object is serialized and saved to disk. Therefore when you
restart your system or application, you need only re-apply the commands since the last snapshot, not all of them.
Won't the snapshot be huge, taking forever to be saved in disk? That dependes of what you consider huge. 1Mb,
1Gb, 1024Gb? How fast can your computer save 1Gb of data from memory to your hard-drive? How "huge" is
your data? Most applications will not require a huge database. If you have a User object with 128
bytes, and you have 1 million users, that's less than 130 megabytes of RAM.
Won't the system have to enter read-only mode when saving the snapshot to disk? Yes, unless you are using
a Space4J cluster. However keep in mind that depending on the size of your data and the speed of your
computer, this operation can be really fast.
Is it difficult to write Commands to modify my data? No. This is Java code manipulating Java Collections.
The only catch is that it has to be written inside commands so that it can be re-applied later. Plus Space4J
comes with many ready-to-use commands.
How about indexing? Do I have to iterate through all my data to find what I want? This is a very important point
and Space4J comes with a complete and easy-to-use indexing framework. You can create four different types
of indexes to search your data in any way you want.
Can I use Space4J with a regular relational database? Yes. Just code SQL statements inside your commands. Then
read all commands from the logs and apply their SQL statement to a relational database. You will end up with
the same data set you have in your Space also inside your relational database tables. Perhaps you
want to use your database for offline work, data warehousing, reports, etc.
Why would I ever need a Space4J cluster? For more advanced systems, a cluster will give you load balance and it will allow
the system to perform a snapshot without having to enter read-only mode. A classical example would be a web application
in load balance, where every web server would have its own Space4J node from a cluster. Setting up Space4J to run in
a cluster is very easy, requiring nothing from the developer.
Can I modify my data while someone else is reading it? Yes! Space4J is like any modern relational database.
Writers only block writers, Readers don't block or get blocked by anything!.
This means that modifications are done one at a time while read-access operations are done concurrently
without any ConcurrentModification exceptions! (Thanks to the new Java 1.6 concurrent data structures!)
How Space4J compares to Prevalyer? Space4J and Prevayler are two free Java implementations of the same concept.
Prevayler has the merit of being the first implementation created from scratch by Klaus Wuestefeld. Klaus
also has the merit of pushing the idea of a prevalent system that is both possible and desirable in many cases.
Although Space4J and Prevalyer are centered in the same idea, they have totally different APIs and implementations.
It is pretty much like Struts and JSF for web frameworks.