Serialization in SQL

Learn the relation and marshaling in the database.

It’s all too common to see RDBMS mentioned as a solution to marshaling and unmarshaling in-memory objects, and even distributed computed systems tend to talk about the storage parts for databases. We should talk about transactional systems rather than storage when we want to talk about RDBMS and other transaction technologies. That said, storage is a good name for distributed file systems.

Common Lisp

On this topic, it might be interesting to realize how Lisp introduced print-readably. In Lisp, rather than working with a compiler and then running static binary files, we work with an interactive REPL where the reader and the printer are fully specified parts of the system. Those pieces are meant to be used by Lisp users. Here’s what the common Lisp standard documentation has to say about printing readably:

If print-readably is true, some special rules for printing objects go into effect. Specifically, printing any object O1, produces a printed representation that, when seen by the Lisp reader while the standard readtable is in effect, will produce an object O2 that is similar to O1.

An example of common Lisp

In the following example code, we define a structure with slots of different types: string, float, and integer. Then we create an instance of that structure, with specific values for the three slots, and serialize this instance to string, only to read it back from the string:

(defpackage #:readably
  (:use #:cl))

(in-package #:readably)

(defstruct foo
  (name nil :type (or nil string))
  (x    0.0 :type float)
  (n    0   :type fixnum))

(defun print-and-read ()
  (let ((instance (make-foo :name "bar" :x 1.0 :n 2)))
    (values instance
             (write-to-string instance :escape t :readably t)))))

We can run it using the following script:


The result is, as expected, a couple of very similar instances:

#S(READABLY::FOO :NAME "bar" :X 1.0 :N 2)
#S(READABLY::FOO :NAME "bar" :X 1.0 :N 2)

The first instance is created in the application code from literal strings and numbers, and the second instance has been created by the reader from a string, which could have been read from a file or a network service somewhere.

The discovery of Lisp predates the invention of the relational model by a long shot, and Lisp wasn’t unique in its capacity to read data structure in-memory from external storage.

It’s important to understand which problem can be solved by using a database service, to insist that storing and retrieving values out of and back into memory isn’t a problem for which we need a database system.

Try it yourself

The file has been executed for creating and initializing the instance in the playground below.

Use the following script to run it:

Get hands-on with 1200+ tech skills courses.