Litestack Documentation

repository·master·Indexed 23 days ago

https://github.com/oldmoe/litestack

An all-in-one data infrastructure gem for Ruby and Rails applications that uses SQLite to provide an embedded database (Litedb), cache (Litecache), job queue (Litejob), message broker (Litecable), full-text search (Litesearch), and metrics platform (Litemetric) with a visualization UI (Liteboard).

Tokens
9.3K
Snippets
18
Records
75
Agent score
81%

What's inside litestack

  1. Overview of Litestack benefits

    master

    Litestack is a unified solution for Ruby and Rails web applications that provides a database, caching, and job queueing system. It is built on top of SQLite, leveraging its file-based nature for easy setup and management without requiring separate server installations.

    Key benefits include:

    • Performance & Efficiency: Low memory footprint and optimized database access via SQLite, supplemented by asynchronous job queueing and in-memory caching.
    • Simplicity: Designed as a turnkey solution to minimize infrastructure management.
    • Modern Concurrency: Deep integration with state-of-the-art Ruby IO libraries like Async and Polyphony for improved concurrency and parallelism.
    • Low Overhead: Reduces DevOps complexity by minimizing dependencies and configuration requirements.
  2. Deployment constraints for Litestack

    master
    Litestack is designed for single-host deployment. Because SQLite requires direct access to the database file for performance and safety, you cannot use auto-scaling app servers that distribute traffic across multiple independent hosts. You can scale vertically (using a larger server with more processes), but you must maintain a single application server instance.
  3. Considerations for containerizing Litestack

    master
    Since Litestack uses SQLite, it is a stateful application. Standard container deployment patterns (destroying and recreating immutable containers) can lead to perceived downtime or data management complexities. When using containers, ensure you have a strategy for managing the persistent SQLite database file outside the container lifecycle.
  4. Concurrency in Ruby: GVL and thread management

    master

    Litestack (via the Ruby sqlite3 gem) does not release the Global VM Lock (GVL) while performing queries. This means a query in one thread will prevent other threads in the same Ruby process from resuming until the query returns.

    Recommendation: For low-latency multi-threaded applications, use Fibers instead of Threads. Since execution is effectively serialized by the GVL during database operations, using Fibers avoids the unnecessary overhead of a multi-threaded environment.

  5. Concurrency model: Single writer / multi reader

    master

    Litestack follows the SQLite concurrency model: it allows multiple simultaneous readers, but only one writer at a time.

    Warning: Long-running write operations (such as creating an index on a large table) will block all other write attempts until the operation completes. SQLite does not currently support concurrent index creation.

  6. Integrate Litestack into Ruby on Rails

    master

    Litestack is designed for dead-simple integration with Ruby on Rails. You can transition to a fully functional database, caching, and job queueing solution using the following steps:

    1. Install the gem.
    2. Apply three lines of configuration changes to your Rails application.
  7. Install Litestack

    master

    To use Litestack in a Ruby application, add the gem to your Gemfile and run bundle install.

    For Rails applications, you can run the generator to set up the full Litestack configuration automatically.

    $ bundle add litestack
    
    # For Rails applications
    $ rails generate litestack:install
  8. Choosing a filesystem for Litestack and SQLite

    master

    Litestack relies on SQLite, which can benefit from specific filesystem characteristics. When selecting a filesystem for your Litestack deployment, consider the following trade-offs:

    High Performance (Standard)

    • XFS: Very stable and trusted; offers fast reads and writes.
    • EXT4: Stable and performant; offers fast reads and writes.

    Optimized for Flash/SSD

    • F2FS: Specifically built for solid-state storage. It supports an atomic write mode that is compatible with SQLite. It provides fast reads and reasonably fast durable writes in synchronous mode.

    Copy-on-Write (CoW) & Snapshotting

    CoW filesystems provide advanced features like snapshots and incremental backups, but typically result in slower write performance due to increased page writes.

    • ZFS: Fast reads and fast device cache. Features filesystem-wide snapshotting with send/recv capabilities for backups and supports compression.
    • Btrfs: Fast reads and fast copies (useful for free backups). Offers granular sub-volume snapshotting with send/recv and supports compression.
    • Bcachefs: A newer CoW filesystem with fast reads and fast device cache. It is improving rapidly but currently lacks some common CoW features compared to ZFS or Btrfs.
  9. Litestack core components

    master

    Litestack is a collection of lightweight, integrated components for Ruby applications. The library provides several specialized modules that can be used individually or together:

    • LiteScheduler: For task scheduling.
    • LiteSupport: Core support utilities.
    • LiteMetric: For metrics and monitoring.
    • LiteDb: A lightweight database interface.
    • LiteCache: For caching mechanisms.
    • LiteJob: For background job processing.
    • LiteCable: For real-time communication (ActionCable compatible).
    • LiteKd: For K-D tree based spatial indexing or similar structures.
  10. Configure Litecache as the Rails cache store

    master

    To use Litecache as your Rails cache store, configure it in your environment files (e.g., config/environments/production.rb).

    config.cache_store = :litecache, {path: './path/to/your/cache/file'}
  11. Manage database connections with Liteconnection

    master

    The Litesupport::Liteconnection module provides a managed interface for SQLite connections, including connection pooling, transaction management, and automatic handling of process exiting and forking.

    Key capabilities include:

    • Connection Pooling: Uses create_pooled_connection to manage multiple connections.
    • Transaction Management: The transaction method allows wrapping blocks of code in a database transaction. It supports :immediate mode by default.
    • Automatic Cleanup: Automatically closes connections and prepared statements on process exit.
    • Fork Safety: Uses Litescheduler::ForkListener to restart connections and background threads when a process forks.
    • Environment-aware Configuration: Loads settings from a YAML file based on the current Litesupport.environment (e.g., production, development).
  12. Configure Litejob queues and priorities

    master

    You can define multiple queues and their priorities in litejob.yml (or config/litejob.yml in Rails).

    Queues require a name and a priority (1-10). Adding the token "spawn" tells Litejob to run every job in its own concurrency context (thread or fiber).

    queues:
        - [default, 1]
        - [urgent, 5]
        - [critical, 10, "spawn"]