ETS

 

"helping you remove your bottlenecks

one process at a time..."

by @holsee

No

Shared

Memory

Erlang Processes do not share memory

State

<proc>

Erlang Processes talk via sending and receiving messages

State

<proc:0>

State

<proc:1>

Erlang Processes will only process 1 message at a time and in order

<proc:0>

Next

So if a process contains data that is required by many other processes, it may form a bottleneck as only 1 request can be served at a time

kv_store module

 

kv_store:write(key, value).
kv_store:read(key).

 

KVStore.write(key, value)
KVStore.read(key)

State

<proc>
kv_store

The system is decomposed into lots of processes,

concurrency win!

...but they are all waiting to be served by our kv_store

R

R

R

R

W

W

W

R

W

Readers

Writers

Processes

.oO( uh oh )

Before our message (         ) gets processed we must wait until the other messages that arrived first have been processed.  

kv_store

mailbox

Is there a way to create data structures that will allow concurrent reads and / or writes in Erlang?

Erlang
Term
Storage

In a nutshell

ETS is a module that provides access to a bunch of Erlang BIFs that give us an in memory store for Erlang Terms!

These provide the ability to store very large quantities of data in an Erlang runtime system!

 

Providing constant access time to the data.

O(log n) for ordered sets & bags.

Creation

ets:new(muh_store, Options).

 

:ets.new(:muh_store, options)

Indexes

you can index on a single column 

 

this will be useful for our key!

Options

Option =

    Type |
    Access |
    named_table |
    {keypos, Pos} |
    {heir, Pid :: pid(), HeirData} |
    {heir, none} |
    Tweaks

Type = set | ordered_set | bag | ordered_bag

Access = public | protected | private

Tweaks =
    {write_concurrency, boolean()}  |
    {read_concurrency, boolean()}  |
    compressed

Concurrency Tweaks

There are options to optimise the read and write concurrency over the ETS table

Tweaks =
    {write_concurrency, boolean()}  |
    {read_concurrency, boolean()}

 

 

Good for bursts of READS and WRITES!

ETS

<mod>
kv_store

Now when our readers and our writers invoke our module kv_store they will bypass any single process mailbox and have direct access to the ETS store

R

R

R

R

W

W

W

W

R

What happens with ETS is not fast enough?

Ask this Guy...

Tonights Kata!

Implement a basic key value store in the functional programming language of your choice!


Go as far with this as you want

 

I will show the ETS based Key Value store at the end
(when I write it at the end)!

ETS

By Steven Holdsworth

ETS

A Tour of Erlang Term Storage

  • 291