BRKH Blog

Tarantool Fundamentals: Storage Engines, Replication, and Sharding

Tarantool is an in-memory computing platform that combines an in-memory DBMS and a Lua server in a single platform providing ACID-compliant storage [1]. It comes in two editions: the open-source Community Edition (CE) and the commercial Enterprise Edition (EE) [1].

This article covers Tarantool's core concepts based on the latest official documentation: the data model, storage engines, fibers and cooperative multitasking, the transaction model, replication, sharding, triggers, and the application server role. At the end, there is a practical example of creating your first database.

What Is Tarantool?

Tarantool combines an in-memory DBMS and a Lua server in a single platform providing ACID-compliant storage [1]. Its key characteristics are:

  • Easy handling of OLTP workloads: processes hundreds of thousands of RPS (requests per second) [1].
  • Data integrity: uses write-ahead log (WAL) and data snapshots [1].
  • Cooperative multitasking: transactions are performed in lightweight coroutines with no inter-thread locking [1].
  • Advanced indexing: composite indexes, locale support, and indexing by nested fields and arrays [1].
  • Compute close to data: Lua server and just-in-time compiler on board [1].
  • Durable distributed storage: multiple failover modes and RAFT-based synchronous replication available [1].

Tarantool allows executing code alongside data, which helps increase the speed of operations [1]. Developers can implement any business logic with Lua, and a single Tarantool instance can also receive SQL requests [1].

Tarantool runs on Linux (x86_64, aarch64), macOS (x86_64, aarch64), and FreeBSD (x86_64) [1]. For connectivity, a number of connectors are provided for popular programming languages [1].

Data Model: Spaces and Tuples

Tarantool is a NoSQL database. It stores data in spaces, which can be thought of as tables in a relational database, and tuples, which are analogous to rows [2].

There are six basic data operations in Tarantool. The platform allows describing the data schema but does not require it [2].

Tarantool supports highly customizable indexes of various types [2]. To ensure data persistence and recover quickly in case of failure, Tarantool uses mechanisms like the write-ahead log (WAL) and snapshots [2].

Storage Engines: memtx and vinyl

A storage engine is a set of low-level routines that store and retrieve values [2]. Tarantool offers a choice of two storage engines:

  • memtx is the in-memory storage engine used by default [2].
  • vinyl is the on-disk storage engine [2].

When to choose which

Use memtx as the default for fast in-memory workloads. Choose vinyl when data does not fit in memory and on-disk durability is the priority.

Fibers and Cooperative Multitasking

Tarantool executes code in fibers that are managed via cooperative multitasking [2].

Cooperative multitasking means each fiber only yields control at specific points, so there is no expensive inter-thread interleaving on hot paths [1].

ACID Transactions: Default and MVCC

Tarantool's ACID-compliant transaction model lets the user choose between two modes [2]:

  • Default mode: allows for fast monopolistic atomic transactions. It doesn't support interactive transactions, and in case of an error, all transaction changes are rolled back [2].
  • MVCC mode: relies on a multi-version concurrency control engine that allows yielding within a longer transaction. This mode only works with the default in-memory memtx storage engine [2].

Replication: Asynchronous and Synchronous

Replication allows keeping the data in copies of the same database for better reliability [2]. Several Tarantool instances can be organized in a replica set. They communicate and transfer data via the iproto binary protocol [2].

By default, replication in Tarantool is asynchronous [2]:

A transaction committed locally on the master node may not get replicated onto other instances before the client receives a success response. Thus, if the master reports success and then dies, the client might not see the result of the transaction [2].

With synchronous replication, transactions on the master node are not considered committed or successful before they are replicated onto a number of instances. This is slower, but more reliable [2]. Synchronous replication in Tarantool is based on an implementation of the RAFT algorithm [2].

Sharding with vshard

Tarantool implements database sharding via the vshard module [2].

Sharding divides a large dataset across several instances, so capacity and throughput can scale horizontally beyond a single server.

Triggers and the Application Server

Tarantool allows specifying callback functions that run upon certain database events [2]. They can be useful for resolving replication conflicts [2].

Using Tarantool as an application server, you can write applications in Lua, C, or C++. You can also create reusable modules [2]. To increase the speed of code execution, Tarantool has a Lua Just-In-Time compiler (LuaJIT) on board [2].

LuaJIT compiles hot paths in the code – paths that are used many times – thus making the application work faster [2]. To enable developers to work with LuaJIT, Tarantool provides tools like the memory profiler and the getmetrics module [2].

Practical Example: Your First Database

The example below is taken from the official tutorial "Creating your first Tarantool database" [3].

Setting up the application

Create a tt environment, then create an instance with instances.yml and config.yaml listening on port 3301 at 127.0.0.1.

# instances.enabled/create_db/config.yaml
groups:
  group001:
    replicasets:
      replicaset001:
        instances:
          instance001:
            iproto:
              listen:
              - uri: '127.0.0.1:3301'
# instances.enabled/create_db/instances.yml
instance001:

Start the instance and connect to the interactive console:

tt start create_db
tt status create_db
tt connect create_db:instance001

Creating a space and indexes

box.schema.space.create('bands')

box.space.bands:format({
    { name = 'id', type = 'unsigned' },
    { name = 'band_name', type = 'string' },
    { name = 'year', type = 'unsigned' }
})

box.space.bands:create_index('primary', { parts = { 'id' } })
box.space.bands:create_index('secondary', { parts = { 'band_name' } })

Writing and selecting data

box.space.bands:insert { 1, 'Roxette', 1986 }
box.space.bands:insert { 2, 'Scorpions', 1965 }
box.space.bands:insert { 3, 'Ace of Base', 1987 }

box.space.bands:select { 3 }
box.space.bands.index.secondary:select{'Scorpions'}

Selecting with the primary index returns the tuple [3, 'Ace of Base', 1987], and with the secondary index returns [2, 'Scorpions', 1965] [3].

Community and Enterprise Editions

Tarantool comes in two editions: the open-source Community Edition (CE) and the commercial Enterprise Edition (EE) [1].

Community Edition lets you develop applications and speed up a system in operation. It features synchronous replication, affords easy scalability, and includes tools to develop efficient applications [1].

Enterprise Edition provides advanced tools for administration, deployment, and security management, along with premium support services. This edition includes all the Community Edition features and is more predictable in terms of solution cost and maintenance. The Enterprise Edition is shipped as an SDK and includes a number of closed-source modules [1].

Enterprise-specific features include security audit log, SSL support for traffic encryption, centralized configuration storage, supervised failover, tuple compression, and read views [1].

Sources and References

[1] Tarantool Documentation. Overview. https://www.tarantool.io/en/doc/latest/overview/ [2] Tarantool Documentation. Concepts. https://www.tarantool.io/en/doc/latest/platform/concepts/ [3] Tarantool Documentation. Creating your first Tarantool database. https://www.tarantool.io/en/doc/latest/getting_started/getting_started_db/

Contents