Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 47 ยท SQL and NoSQL on Hadoop

HBase: Wide-Column Storage

Learn how HBase stores huge sparse tables: row keys, column families, regions, the LSM tree with memstore, HFiles and compaction, and good row-key design.

HDFS is excellent at storing big files and scanning them from start to finish. It is bad at one thing: changing one small record in the middle of a file, or reading a single record by key in a few milliseconds. HBase fills that gap. It is a distributed, sorted, wide-column database that stores its data on HDFS and gives you fast random reads and writes on tables with billions of rows.

This lesson explains the HBase data model, how a table is split into regions, and the idea that makes writes so fast: the log-structured merge tree (LSM tree). It follows Pig Latin Dataflows and builds on HDFS Architecture.

HBase cannot run on the site (it needs HDFS, ZooKeeper and several servers). The shell commands are read-only. The runnable examples are stand-ins: a small LSM store written in Python, and a row-key experiment. Both show the real ideas, but not the real code.

The data model

Forget rows and columns as you know them from SQL. An HBase table is best pictured as a sorted map of maps:

TEXT
(row key, column family:qualifier, timestamp) -> value
  • Row key: a byte string. Rows are stored sorted by row key, and that is the only index you get.
  • Column family: a group of columns declared when the table is created (for example info and stats). All columns of a family are stored together on disk. A table usually has only a few families.
  • Column qualifier: the column name inside a family (info:name, info:email). Qualifiers are not declared in advance; each row can have different ones. That is why the table is sparse: a missing cell costs nothing.
  • Timestamp / version: every cell keeps several versions, newest first. By default the timestamp is the write time.

HBase in one picture

Row key The only index. Rows are sorted by it.
Column family A few, fixed at table creation.
Qualifier Free-form column name per row.
Version Each cell keeps timestamped values.

A cell is addressed by row, family:qualifier and time.

1 / 2

Here is a small session in the HBase shell. This is real syntax, shown read-only:

TEXT
create 'users', 'info', 'stats'
put 'users', 'u1001', 'info:name', 'Mina'
put 'users', 'u1001', 'info:city', 'Lyon'
put 'users', 'u1001', 'stats:logins', '42'
get 'users', 'u1001'
scan 'users', {STARTROW => 'u1000', STOPROW => 'u2000', LIMIT => 10}
delete 'users', 'u1001', 'info:city'

Note that get and scan work on row-key ranges. There are no joins, no secondary indexes and no SQL by default (Apache Phoenix adds SQL on top). You design the table around the queries you will ask.

Regions and the cluster

A table is cut by row-key range into regions. A region holds a contiguous slice such as u1000 up to u2000. When a region grows past a size limit it splits into two. Regions are served by RegionServers, and one RegionServer typically serves dozens or hundreds of regions.

  • The HMaster assigns regions to RegionServers, handles schema changes and rebalances.
  • ZooKeeper tracks which servers are alive and where the hbase:meta table is, a special table that maps row-key ranges to RegionServers.
  • A client asks ZooKeeper and the meta table once, caches the answer, and from then on talks directly to the RegionServer that owns the row. The HMaster is not on the read/write path.

The RegionServer writes its files into HDFS, so replication and durability of the bytes come from HDFS replication. If a RegionServer dies, the HMaster hands its regions to other servers, which just open the files that are already in HDFS.

flowchart LR
    C["Client"] -->|"1. where is row u1500?"| Z["ZooKeeper +<br/>hbase:meta"]
    Z -->|"2. RegionServer 2"| C
    C -->|"3. get / put"| R["RegionServer 2<br/>(region u1000-u2000)"]
    R --> H["HDFS files"]

The client finds the region once and then talks to the RegionServer directly.

The write path: an LSM tree

Updating a sorted file in place is slow, because it means random disk writes. HBase avoids this with an LSM tree, in three steps.

  1. WAL (write-ahead log). The RegionServer first appends the edit to a log on HDFS. If the server crashes, the log is replayed. Appending is fast.
  2. MemStore. The edit is then put in an in-memory sorted structure, one per column family per region. The write is now acknowledged.
  3. Flush to an HFile. When a MemStore reaches a size limit (128 MB by default), it is written to HDFS as a new immutable, sorted HFile. The matching WAL part can be dropped.

Reads must therefore look in the MemStore and in several HFiles, newest first, and merge what they find. Deletes never edit a file: HBase writes a tombstone marker that hides older versions. Over time the number of HFiles grows, which would make reads slow, so the server runs compaction:

  • Minor compaction merges a few small HFiles into a bigger one.
  • Major compaction rewrites all HFiles of a family into one, and finally drops tombstones and expired versions.

To avoid opening every HFile for a point read, each HFile has a Bloom filter (a compact structure that says "this row is definitely not in this file" or "maybe") plus a block index. A BlockCache keeps hot blocks in memory.

The trade-off is typical for LSM trees: writes are cheap (a sequential append plus memory), reads cost more (merge several sources), and background compaction burns disk and network I/O. Compare this with the B-tree in a classic database, which updates pages in place.

The next stand-in is a tiny LSM store. It has a memtable, flushes to immutable sorted "files", reads newest first, uses tombstones and compacts.

Python A mini LSM store with flush and compaction

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the output carefully. u2 shows the newer value because the store checks the memtable and the newest file first. u3 is gone once its tombstone is found, and after compaction the old data and the tombstone vanish for good.

Designing row keys

Because the row key is your only index, it is the most important design decision in HBase. Three rules cover most cases.

  • Avoid hot spots. Sorted keys that always increase (a timestamp, an auto-increment id) send every new write to the last region, so one server does all the work. Fix it by salting (a small hash prefix, such as hash(id) % 16), by hashing, or by putting a well-spread field first.
  • Put what you scan together. If you want "all events of one user, newest first", build the key as userId + reversed timestamp (for example Long.MAX_VALUE - ts). One range scan then returns exactly that user's data, latest first.
  • Keep keys short. The key is stored in every cell of the row, so long keys waste space everywhere.

There is a price for salting: a scan over a range now has to run once per salt bucket and merge the results. Choose the trade-off that matches your reads.

Python Row keys, regions and hot spots

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The sequential keys all land in one region, while the salted keys spread over all four. The reversed-timestamp keys sort so that a scan returns the newest event first.

When to choose HBase

HBase is a good fit for very large tables with key-based access, high write rates, and sparse columns: time series, messaging, user profiles, click and sensor data. It is a poor fit for ad-hoc analytics (use Hive with a columnar format), for joins and multi-row transactions, or for small data that a single PostgreSQL server handles easily. HBase has strong consistency per row (one RegionServer owns a row), which differs from the eventual consistency of Dynamo-style stores.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the only index an HBase table has by default?

    Rows are stored sorted by row key, so gets and range scans on the row key are fast. Everything else needs a different table design or a layer such as Phoenix.
  2. 2. What is the order of an HBase write?

    The write-ahead log makes the edit durable first, the MemStore makes it readable and sorted, and a flush later writes an immutable HFile.
  3. 3. Why does a Bloom filter per HFile help?

    A Bloom filter answers 'definitely not here' or 'maybe here', so most HFiles are never opened for a get.
  4. 4. A table uses a plain increasing timestamp as the row key. What is the likely problem?

    Ascending keys always land at the end of the key range, so a single RegionServer receives all writes. Salting or hashing spreads the load.

Summary

  • HBase is a sorted, sparse, versioned wide-column store on HDFS for fast random reads and writes by row key.
  • A table is split into regions served by RegionServers; clients find them through ZooKeeper and hbase:meta, then talk to the RegionServer directly.
  • Writes go to the WAL and MemStore, flush to immutable HFiles and are cleaned up by minor and major compactions; deletes are tombstones.
  • Bloom filters and the block cache keep reads fast despite many files.
  • Row-key design decides performance: avoid hot spots, keep scanned data together, keep keys short.

Next: Choosing a NoSQL Store.