• Stars
    star
    238
  • Rank 169,306 (Top 4 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 6 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An In-Memory Cache Backed by Apache Kafka

KCache - An In-Memory Cache Backed by Apache Kafka

Build Status Maven Javadoc

KCache is a client library that provides an in-memory cache backed by a compacted topic in Kafka. It is one of the patterns for using Kafka as a persistent store, as described by Jay Kreps in the article It's Okay to Store Data in Apache Kafka.

Maven

Releases of KCache are deployed to Maven Central.

<dependency>
    <groupId>io.kcache</groupId>
    <artifactId>kcache</artifactId>
    <version>5.0.0</version>
</dependency>

For Java 11 or above, use 5.x otherwise use 4.x.

Usage

An instance of KafkaCache implements the java.util.SortedMap interface. Here is an example usage:

import io.kcache.*;

String bootstrapServers = "localhost:9092";
Cache<String, String> cache = new KafkaCache<>(
    bootstrapServers,
    Serdes.String(),  // for serializing/deserializing keys
    Serdes.String()   // for serializing/deserializing values
);
cache.init();   // creates topic, initializes cache, consumer, and producer
cache.put("Kafka", "Rocks");
String value = cache.get("Kafka");  // returns "Rocks"
cache.remove("Kafka");
cache.close();  // shuts down the cache, consumer, and producer

One can also use RocksDB to back the KafkaCache:

Properties props = new Properties();
props.put(KafkaCacheConfig.KAFKACACHE_BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(KafkaCacheConfig.KAFKACACHE_BACKING_CACHE_CONFIG, "rocksdb");
props.put(KafkaCacheConfig.KAFKACACHE_DATA_DIR_CONFIG, "/tmp");
Cache<String, String> cache = new KafkaCache<>(
    new KafkaCacheConfig(props),
    Serdes.String(),  // for serializing/deserializing keys
    Serdes.String()   // for serializing/deserializing values
);
cache.init();

Basic Configuration

KCache has a number of configuration properties that can be specified.

  • kafkacache.bootstrap.servers - A list of host and port pairs to use for establishing the initial connection to Kafka.
  • kafkacache.group.id - The group ID to use for the internal consumer. Defaults to kafkacache.
  • kafkacache.client.id - The client ID to use for the internal consumer. Defaults to kafka-cache-reader-<topic>.
  • kafkacache.topic - The name of the compacted topic. Defaults to _cache.
  • kafkacache.topic.replication.factor - The desired replication factor for the compacted topic. Defaults to 3.
  • kafkacache.topic.num.partitions - The desired number of partitions for for the compacted topic. Defaults to 1.
  • kafkacache.topic.partitions - A list of partitions to consume, or all partitions if not specified.
  • kafkacache.topic.partitions.offset - The offset to start consuming all partitions from, one of beginning, end, a positive number representing an absolute offset, a negative number representing a relative offset from the end, or @<value>, where <value> is a timestamp in ms. Defaults to beginning.
  • kafkacache.init.timeout.ms - The timeout for initialization of the Kafka cache, including creation of the compacted topic. Defaults to 300 seconds.
  • kafkacache.timeout.ms - The timeout for an operation on the Kafka cache. Defaults to 60 seconds.
  • kafkacache.backing.cache - The backing cache for KCache, one of memory (default), bdbje, caffeine, lmdb, mapdb, rdbms, or rocksdb.
  • kafkacache.data.dir - The root directory for backing cache storage. Defaults to /tmp.

Configuration properties can be passed as follows:

Properties props = new Properties();
props.setProperty("kafkacache.bootstrap.servers", "localhost:9092");
props.setProperty("kafkacache.topic", "_mycache");
Cache<String, String> cache = new KafkaCache<>(
    new KafkaCacheConfig(props),
    Serdes.String(),  // for serializing/deserializing keys
    Serdes.String()   // for serializing/deserializing values
);
cache.init();
...

Security

KCache supports both SSL authentication and SASL authentication to a secure Kafka cluster. See the JavaDoc for more information.

Using KCache as a Replicated Cache

KCache can be used as a replicated cache, with some caveats. To ensure that updates are processed in the proper order, one instance of KCache should be designated as the sole writer, with all writes being forwarded to it. If the writer fails, another instance can then be elected as the new writer.

For an example of a highly-available service that wraps KCache, see Keta.

More Repositories

1

kareldb

A Relational Database Backed by Apache Kafka
Java
392
star
2

hgraphdb

HBase as a TinkerPop Graph Database
Java
251
star
3

generator-angular-flask

Yeoman generator for AngularJS + Flask
Python
204
star
4

generator-angular-go-martini

Yeoman generator for AngularJS + Go + Martini
JavaScript
186
star
5

awesome-hbase

A curated list of awesome HBase projects and resources.
161
star
6

MicroFrameworkRosettaStone

A comparison of a number of web micro-frameworks via code generation
142
star
7

kafka-graphs

Graph Analytics with Apache Kafka
Java
101
star
8

generator-angular-express-sequelize

Yeoman generator for AngularJS + Express + Sequelize
JavaScript
81
star
9

generator-angular-dropwizard

Yeoman generator for AngularJS + Dropwizard
JavaScript
66
star
10

generator-angular-slim

Yeoman generator for AngularJS + Slim
JavaScript
61
star
11

generator-angular-scotty

Yeoman generator for AngularJS + Scotty
JavaScript
54
star
12

generator-angular-sinatra

Yeoman generator for AngularJS + Sinatra
JavaScript
44
star
13

kwack

In-Memory Analytics for Kafka using DuckDB
Java
36
star
14

generator-angular-spark

Yeoman generator for AngularJS + Spark
JavaScript
34
star
15

kdatalog

Kafka as a Datalog Engine
Java
27
star
16

kgiraffe

A GraphQL Interface for Apache Kafka and Schema Registry
Java
24
star
17

hdocdb

HBase as a JSON Document Database
Java
24
star
18

keta

A Transactional Metadata Store Backed by Apache Kafka
Java
19
star
19

generator-angular-nancy

Yeoman generator for AngularJS + Nancy
JavaScript
16
star
20

generator-angular-scalatra

Yeoman generator for AngularJS + Scalatra
JavaScript
16
star
21

generator-angular-luminus

Yeoman generator for AngularJS + Luminus
JavaScript
12
star
22

schema-registry-browser

Confluent Schema Registry Browser
Vue
12
star
23

generator-angular-mojolicious

Yeoman generator for AngularJS + Mojolicious
JavaScript
12
star
24

kafka-connect-streams

Kafka Connect Integration with Kafka Streams + KSQL
Java
11
star
25

stream-processing-kickstarter

A comparison of stream-processing frameworks with Kafka integration
Java
10
star
26

generator-angular-ratpack

Yeoman generator for AngularJS + Ratpack
JavaScript
10
star
27

janusgraph-kafka

Kafka storage adapter for JanusGraph
Java
9
star
28

generator-angular-dynamo

Yeoman generator for AngularJS + Dynamo
JavaScript
9
star
29

generator-angular-suave

Yeoman generator for AngularJS + Suave
JavaScript
8
star
30

kmachines

Distributed Fine-Grained Finite State Machines with Kafka
Java
8
star
31

generator-angular-caveman2

Yeoman generator for AngularJS + Caveman2
JavaScript
7
star
32

provision-angular-flask

Ansible provisioner for AngularJS + Flask
6
star
33

hentitydb

HBase as an Entity Database
Java
6
star
34

jsonata-python

JSONata for Python
Python
6
star
35

generator-angular-opium

Yeoman generator for AngularJS + Opium
JavaScript
6
star
36

provision-angular-go-martini

Ansible provisioner for AngularJS + Go + Martini
5
star
37

schema-registry-chess-engine

Confluent Schema Registry Chess Engine
Java
5
star
38

kafka-connect-jsonata

Kafka Connect JSONata Transform
Java
5
star
39

kstore

A Wide Column Store Backed by Apache Kafka
Java
4
star
40

schema-registry-mode-plugin

Confluent Schema Registry Subject Modes
Java
4
star
41

provision-angular-express-sequelize

Ansible provisioner for AngularJS + Express + Sequelize
3
star
42

generator-angular-nickel

Yeoman generator for AngularJS + Nickel
JavaScript
3
star
43

janusgraph-cosmosdb

The Azure Cosmos DB Storage Backend for JanusGraph
Java
3
star
44

generator-angular-axiom

Yeoman generator for AngularJS + Axiom
JavaScript
3
star
45

provision-angular-dropwizard

Ansible provisioner for AngularJS + Dropwizard
3
star
46

generator-angular-orbit

Yeoman generator for AngularJS + Orbit
JavaScript
2
star
47

kstore-shell

HBase Shell for KStore
Shell
2
star
48

cel.net

Common Expression Language for .NET
Starlark
2
star
49

generator-angular-spin

Yeoman generator for AngularJS + Spin
JavaScript
2
star
50

provision-angular-spark

Ansible provisioner for AngularJS + Spark
2
star
51

generator-angular-chinook

Yeoman generator for AngularJS + Chinook
JavaScript
2
star
52

json-schema-compatibility

Java
2
star
53

provision-angular-caveman2

Ansible provisioner for AngularJS + Caveman2
2
star
54

generator-angular-kitura

Yeoman generator for AngularJS + Kitura
JavaScript
2
star
55

provision-angular-scotty

Ansible provisioner for AngularJS + Scotty
1
star
56

generator-aurelia-dropwizard

Yeoman generator for Aurelia + Dropwizard
JavaScript
1
star
57

generator-angular-start

Yeoman generator for AngularJS + Start
JavaScript
1
star
58

provision-angular-luminus

Ansible provisioner for AngularJS + Luminus
1
star
59

maestro

A Dropwizard service for running orchestrations.
JavaScript
1
star
60

provision-angular-mojolicious

Ansible provisioner for AngularJS + Mojolicious
1
star
61

demo-data-contracts

Java
1
star