# RethinkDB Realtime Database

> RethinkDB document database with real-time change feeds.

- Category: Database Stacks
- Difficulty: beginner
- Components: rethinkdb
- Tags: rethinkdb, realtime, document, nosql, changefeeds
- Source: https://docker.recipes/databases/rethinkdb-realtime

## Overview

RethinkDB is an open-source NoSQL document database engineered from the ground up for real-time applications. Unlike traditional databases that require polling for updates, RethinkDB features built-in change feeds that push live updates to applications whenever data changes. Developed by the RethinkDB team and later maintained by the community, it uses the ReQL (RethinkDB Query Language) which allows developers to write expressive queries that can subscribe to changes and automatically receive notifications when documents are modified, inserted, or deleted. This Docker configuration deploys a single RethinkDB instance with the web administration interface exposed on port 8080, the driver port on 28015 for client connections, and the cluster port on 29015 for distributed deployments. RethinkDB excels in scenarios requiring live data synchronization such as collaborative applications, real-time analytics dashboards, and chat systems where users need immediate updates without refresh cycles. This setup is ideal for developers building real-time web applications, startups prototyping collaborative tools, and teams needing a database that can handle both complex queries and live data feeds. The combination of RethinkDB's intuitive query language, automatic change notifications, and built-in web interface makes it particularly valuable for rapid development of applications that require instant data synchronization across multiple clients.

## Features

- Real-time change feeds that automatically push updates to subscribed queries
- ReQL query language with chainable commands and built-in real-time capabilities
- Web-based administration interface for database management and query testing
- Automatic document indexing with support for compound and multi-dimensional indexes
- Built-in map-reduce functionality for aggregating data across documents
- Atomic operations and transactions within single documents
- Geographic query support with built-in geospatial indexing
- Horizontal scaling capabilities with automatic data distribution

## Use cases

- Real-time chat applications requiring instant message delivery and presence updates
- Collaborative editing platforms like document editors or design tools with live collaboration
- Live analytics dashboards displaying real-time metrics and business intelligence
- Multiplayer gaming backends tracking player state and game events
- IoT data collection systems processing sensor data streams with live monitoring
- Social media feeds requiring instant updates for likes, comments, and posts
- Financial trading platforms needing real-time price updates and transaction monitoring

## Prerequisites

- Docker and Docker Compose installed with minimum 2GB available RAM for RethinkDB operations
- Ports 8080, 28015, and 29015 available on the host system
- Basic understanding of NoSQL document databases and JSON data structures
- Familiarity with ReQL syntax for writing queries and change feed subscriptions
- Understanding of real-time application architecture and WebSocket connections
- Knowledge of JavaScript or Python for client-side RethinkDB driver integration

## docker-compose.yml

```yaml
services:
  rethinkdb:
    image: rethinkdb:latest
    container_name: rethinkdb
    restart: unless-stopped
    ports:
      - "${ADMIN_PORT:-8080}:8080"
      - "${DRIVER_PORT:-28015}:28015"
      - "${CLUSTER_PORT:-29015}:29015"
    volumes:
      - rethinkdb_data:/data
    command: rethinkdb --bind all
    networks:
      - rethinkdb-network

volumes:
  rethinkdb_data:

networks:
  rethinkdb-network:
    driver: bridge
```

## .env

```bash
# RethinkDB
ADMIN_PORT=8080
DRIVER_PORT=28015
CLUSTER_PORT=29015
```

## Usage notes

- Admin UI at http://localhost:8080
- Driver port at 28015
- Real-time changefeeds
- ReQL query language

## Troubleshooting

- RethinkDB won't start with 'Insufficient memory' error: Increase Docker memory allocation to at least 2GB or add --cache-size 512 to the command
- Web interface shows 'Connection failed' at localhost:8080: Verify the container is running and ADMIN_PORT environment variable matches your expected port
- Client connections fail to port 28015: Check DRIVER_PORT environment variable and ensure no firewall is blocking the connection
- Change feeds stop working after container restart: Restart client applications as change feed subscriptions don't persist across database restarts
- High memory usage during large queries: Add --max-cache-size flag to the RethinkDB command to limit memory consumption
- Cluster connection issues on port 29015: Ensure CLUSTER_PORT is properly exposed and not conflicting with other services

