A mobile game produces a score every time a player finishes a round, and players expect the leaderboards to move as they play. In this post, a simulation plays the game and sends each score to Kafka. Four Flink SQL jobs keep four top 10 leaderboards up to date in PostgreSQL, and a web dashboard shows them as they change. Everything runs on your own machine.

[Update 2026-11-15] The simulation is now one YAML file, leaderboard/simulation/game.yaml, which desflow 1.0.0 runs with desflow run. It replaces run.py, game.py, the score model and the serializer, and the Avro schema is now the file score.avsc. The code, commands, log and screenshots below show the new version, and Step 4 now says when robots reach the hot streaks.

[Update 2026-10-16] dynamic-des is now desflow (0.20.0). The code and commands below use the new name: pip install desflow, import desflow and the desflow command.

The source code is in benchtop/game-leaderboard. It is one of the Benchtop projects, which run locally from a fresh clone.

What You Will Build

  1. Run the simulation, which plays the game and sends each score to Kafka.
  2. Submit the four Flink jobs, which keep the leaderboards up to date in PostgreSQL.
  3. Open the dashboard, which shows the four leaderboards as they change.
  4. Change the game while it runs, and watch robots take over the top players and the team MVPs.
  5. Clean up, to start again.

To start again at any point, python -m leaderboard.stores.cleanup removes everything this project has created and keeps the services running. Clean Up describes what it removes.

The four leaderboards:

LeaderboardWhat it shows
Top teamsthe 10 teams with the highest total score
Top playersthe 10 players with the highest total score
Hot streaksthe 10 players scoring furthest above their usual rate: their average score over the last 10 seconds divided by their average over the last 60 seconds
Team MVPseach team’s top scorer and their share of the team’s total, then the 10 players with the largest shares

Architecture

Architecture: a simulated game sends scores to Kafka, Flink SQL jobs keep leaderboards in PostgreSQL, and a dashboard shows them

  1. Events. A simulation built with desflow (documentation) plays the game in real time. desflow is a Python library for simulations that stream their output. Each score is published to the Kafka topic game-scores in Avro, a compact binary format whose schema is kept in a schema registry.
  2. Leaderboards. Four Flink SQL jobs, one per leaderboard, read the topic and compute the leaderboards. Flink is a stream processor. It keeps each query’s result up to date as events arrive, rather than running the query once.
  3. Storage. Each job writes its leaderboard to a PostgreSQL table with one row per rank. When the team or player at a rank changes, Flink updates that row.
  4. Dashboard. A NiceGUI web page reads the four tables and redraws them every 2 seconds. NiceGUI is a Python library for web pages. The page shows the current leaderboards as soon as it opens, because the tables always hold the latest state.

The tools:

ToolRole here
desflowsimulates the game, publishes the events to Kafka, and reads parameter changes from Kafka while it runs
Apache Kafka, with Karapacecarries the events; Karapace is the schema registry that holds their Avro schema
Apache Flinkruns the SQL jobs that compute the leaderboards
PostgreSQLstores the leaderboards
NiceGUIthe dashboard
odctlstarts all the services above with Docker Compose

Setup

You need Docker (Docker Desktop, OrbStack or Docker Engine), uv and Python 3.13. Clone the repository and run every command from the project folder:

1git clone https://github.com/jaehyeon-kim/benchtop.git
2cd benchtop/game-leaderboard
3
4uv venv                             # create .venv
5source .venv/bin/activate           # activate it, in each new shell
6uv pip install -r requirements.txt
7
8odctl up kafka-lite flink-lite postgres

odctl is a command line tool that starts a local data stack with Docker Compose. kafka-lite starts one Kafka broker, Karapace and Kafka UI. flink-lite starts a Flink cluster with one TaskManager, the process that runs a job’s work. postgres starts PostgreSQL.

The web UIs:

  • Kafka UI, for the topic and its messages: http://127.0.0.1:8086
  • Flink UI, for the running jobs: http://127.0.0.1:8082

Simulation Model

A discrete-event simulation (DES) moves a clock from one event to the next, such as a player’s next round. Nothing happens between events, so the model only has to say what each event does and how long it is until the next one. Here the clock runs at the speed of real time.

The model is one YAML file, leaderboard/simulation/game.yaml, built from desflow’s parts. An entity is one player, and a task is one step it takes. Each task can send a record when it finishes:

Part of the gamedesflow featureDefault
Players arrive at randoman arrival, player: exponential gapsa player every 2 seconds
A player’s sessionan entity per player, which goes through the tasks join_team, then tick and round in turn, then leave_teamlasts a lognormal time, 5 minutes on average
Time between roundsa service, round for people and robot_round for robots6 seconds for people, 1.5 for robots
A device goes offlinea task, late, which holds the score for the service offline, then sends it with the time it was earned7 minutes on average
Robots and late scoresregistry variables, robot_share and late_share5% of players, 1% of scores
Sending the scoresKafka egress with envelope: false, which sends only the scoreabout 27 scores a second
Changing parametersKafka ingress on game-controlnone

The services, the Kafka writer and reader, and the arrival come after the variables. When a player arrives, the arrival decides whether they are a robot, names them USR- or RBT- with a number, and draws their session length. The Kafka writer sends each score in Avro, with the schema in score.avsc:

 1services:
 2  session: {dist: lognormal, mean: 300, std: 120}   # 5 minutes
 3  round: {dist: lognormal, mean: 6, std: 2}
 4  robot_round: {dist: normal, mean: 1.5, std: 0.3}
 5  offline: {dist: normal, mean: 420, std: 120}      # 7 minutes
 6
 7# Flink's source table reads the score's own fields, so the Avro value is the score alone,
 8# registered under the subject game-scores-value. The topic is created on first use.
 9egress:
10  - type: Kafka
11    when: events
12    envelope: false
13    batch_size: 25   # about a second of scores
14    config:
15      bootstrap_servers: "${KAFKA_BROKER:-127.0.0.1:9092}"
16      event_topic: game-scores
17      serializer:
18        type: confluent_avro
19        registry_url: "${REGISTRY_URL:-http://127.0.0.1:8081}"
20        schema_file: score.avsc
21
22# Parameter changes from `control`; the ingress creates the topic if it is missing
23ingress:
24  - type: Kafka
25    config: {bootstrap_servers: "${KAFKA_BROKER:-127.0.0.1:9092}", topic: game-control}
26
27arrivals:
28  player:
29    dist: exponential
30    rate: 0.5   # a player every 2 seconds on average
31    spawn: join_team
32    vars:
33      robot:
34        gen: boolean
35        p: {gen: param, path: game.variables.robot_share}
36    # USR- and a number for a person, RBT- for a robot
37    key:
38      gen: format
39      template: "{p}-{n:06d}"
40      names:
41        p: {gen: when, condition: robot, then: RBT, else: USR}
42        n: {gen: sequence, start: 1}
43    attributes:
44      robot: {gen: var, name: robot}
45      born: {gen: expr, expr: t}
46      session_len: {gen: sample, service: session}

A session starts with join_team, which puts the player in a team with room, or forms a new team. A team holds up to 15 players, and one arrival in ten forms a new team anyway. A new team takes the next 10-digit id and a free name. When no name is free, the player joins a team with room, and when no team has room either, the player is turned away. The teams playing now are registry variables, which join_team and leave_team update. The player then plays rounds until the session time is up, and each round earns a score from 0 to 20. tick waits one round and draws the score, and round sends it. A late score is sent minutes later by late, a task of its own, with the time it was earned. When the session ends, leave_team takes the player out of their team, and a team with no players left dissolves. The tasks that play the rounds:

 1  # Wait one round, then leave when the session has ended (before any score), else draw
 2  # the score
 3  tick:
 4    service_time:
 5      gen: expr
 6      expr: "robot_round if attr.robot else human_round"
 7      names:
 8        robot_round: {gen: sample, service: robot_round}
 9        human_round: {gen: sample, service: round}
10    vars:
11      is_late:
12        gen: boolean
13        p: {gen: param, path: game.variables.late_share}
14      score: {gen: integer, bounds: [0, 20]}
15      ended: {gen: expr, expr: "t - attr.born > attr.session_len"}
16    payload: null
17    set:
18      score: {gen: var, name: score}
19    # A late score is sent by its own process with the time it was earned, and the
20    # session goes on
21    spawn:
22      task: late
23      count: {gen: expr, expr: "1 if is_late and not ended else 0"}
24      args:
25        player: {gen: entity}
26        team_id: {gen: attr, name: team_id}
27        team_name: {gen: attr, name: team}
28        score: {gen: var, name: score}
29        earned_ms:
30          gen: format_time
31          format: epoch_ms
32          time: {gen: now}
33    next:
34      when:
35        - {condition: "ended", task: leave_team}
36        - {condition: "is_late", task: tick}
37      default: round
38  round:
39    payload:
40      user_id: {gen: entity}
41      team_id: {gen: attr, name: team_id}
42      team_name: {gen: attr, name: team}
43      score: {gen: attr, name: score}
44      event_time_millis:
45        gen: format_time
46        format: epoch_ms
47        time: {gen: now}
48      event_type: normal
49    next: tick
50  late:
51    service: offline
52    key: {gen: expr, expr: arg.player}
53    payload:
54      user_id: {gen: expr, expr: arg.player}
55      team_id: {gen: expr, expr: arg.team_id}
56      team_name: {gen: expr, expr: arg.team_name}
57      score: {gen: expr, expr: arg.score}
58      event_time_millis: {gen: expr, expr: arg.earned_ms}
59      event_type: late

Every parameter is a path in desflow’s registry, such as game.variables.robot_share or game.service.round.mean. The registry is a store of named parameters. The tasks read the live value each time they draw one, so a change sent to game-control applies from the next arrival or round.

Step 1: Run the Simulation

1desflow run leaderboard/simulation/game.yaml     # Ctrl + C to stop

desflow creates three Kafka topics if they are missing: game-scores for the scores, game-control for parameter changes, and sim-telemetry, desflow’s telemetry topic, to which the game sends nothing. It then plays the game in real time and sends about 27 scores a second until you stop it. The first score registers the Avro schema in Karapace, under the subject game-scores-value. Its log shows the simulation starting, its Kafka writer and reader connecting, and the schema being registered:

18:40:01 [INFO] desflow.builder.context: Building SimulationContext for 'game'...
18:40:01 [INFO] desflow.builder.context: No random_seed was given, so this run uses 3968378124. Repeat it with: desflow run leaderboard/simulation/game.yaml --seed 3968378124
18:40:01 [INFO] desflow.core.environment: KafkaEgress has envelope: false, so it skips telemetry and the queued and started records, and writes each event's value alone.
18:40:01 [INFO] desflow.builder.context: Simulation engine started.
18:40:01 [INFO] desflow.connectors.egress.kafka: Kafka Egress producer connected successfully.
18:40:01 [INFO] aiokafka.consumer.subscription_state: Updating subscribed topics to: frozenset({'game-control'})
18:40:01 [INFO] aiokafka.consumer.group_coordinator: Metadata for topic has changed from {} to {'game-control': 1}. 
18:40:01 [INFO] desflow.connectors.ingress.kafka: Connected to Kafka Ingress topic: game-control
18:40:15 [INFO] httpx: HTTP Request: POST http://127.0.0.1:8081/subjects/game-scores-value/versions "HTTP/1.1 200 OK"

In Kafka UI, the topic game-scores shows the scores. Each message is keyed by the player:

 1{
 2 "key": "USR-000002",
 3 "value": {
 4  "user_id": "USR-000002",
 5  "team_id": "0000000001",
 6  "team_name": "Ruby-Wombat",
 7  "score": 8,
 8  "event_time_millis": 1790771752614,
 9  "event_type": "normal"
10 }
11}

Kafka UI showing the newest game-scores messages, decoded with their Avro schema
Score events on the game-scores topic

In a second terminal, with the environment activated:

1python -m leaderboard.jobs.submit

It creates the PostgreSQL schema game with the four leaderboard tables in leaderboard/jobs/tables.sql, then submits the four jobs:

2026-09-30 22:41:37,300 leaderboard.stores.flink Submitted 01-top-teams
2026-09-30 22:41:41,745 leaderboard.stores.flink Submitted 02-top-players
2026-09-30 22:41:46,280 leaderboard.stores.flink Submitted 03-hot-streaks
2026-09-30 22:41:50,970 leaderboard.stores.flink Submitted 04-team-mvps

Before submitting, it also copies the OpenLineage client library into Flink’s lib folder. Flink’s JDBC connector, which writes to PostgreSQL, needs it, and odctl’s Flink image has a copy but not in that folder.

The jobs keep running in the Flink UI, as game-top-teams, game-top-players, game-hot-streaks and game-team-mvps. Each reads the topic from its first event, so the leaderboards include everything simulated so far.

Flink UI listing the four leaderboard jobs, all running
The four Flink SQL jobs

Shared Tables

Flink SQL reads and writes tables that are defined over external systems. leaderboard/jobs/00-ddl.sql defines the source table scores, which reads the Kafka topic, and four sink tables, each writing to its PostgreSQL table. odctl’s Flink keeps table definitions only for one SQL client session, so each job loads this file first, with sql-client.sh -i. The source table:

 1-- The score events. Event time comes from the event itself, and the watermark lets
 2-- scores arrive up to 5 seconds out of order. Late events, minutes behind, still count
 3-- towards the totals, but the hot streaks' time windows leave them out.
 4CREATE TABLE scores (
 5  user_id           STRING,
 6  team_id           STRING,
 7  team_name         STRING,
 8  score             INT,
 9  event_time_millis BIGINT,
10  event_type        STRING,
11  event_time AS TO_TIMESTAMP_LTZ(event_time_millis, 3),
12  WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND
13) WITH (
14  'connector' = 'kafka',
15  'topic' = 'game-scores',
16  'properties.bootstrap.servers' = 'broker-1:19092',
17  'scan.startup.mode' = 'earliest-offset',
18  'format' = 'avro-confluent',
19  'avro-confluent.url' = 'http://karapace:8081'
20);

Each event carries the time it happened, and Flink uses that time rather than the time the event arrives. This is called event time. A watermark tells Flink how long to wait for events that arrive out of order, here 5 seconds.

A sink table has the rank as its primary key. The key makes the sink an upsert: Flink replaces the row at a rank when it changes.

 1CREATE TABLE top_teams (
 2  rnk BIGINT, team_id STRING, team_name STRING, total_score BIGINT,
 3  PRIMARY KEY (rnk) NOT ENFORCED
 4) WITH (
 5  'connector' = 'jdbc',
 6  'url' = 'jdbc:postgresql://postgres:5432/odctl',
 7  'table-name' = 'game.top_teams',
 8  'username' = 'user',
 9  'password' = 'password',
10  'sink.buffer-flush.interval' = '1s'
11);

One Job per Leaderboard

Each leaderboard has its own job file, from 01-top-teams.sql to 04-team-mvps.sql. Each file sets its own job name and settings, then runs one INSERT query. The top teams job, leaderboard/jobs/01-top-teams.sql:

 1-- One leaderboard job. Submit it with the shared table definitions:
 2--   ./bin/sql-client.sh -i /tmp/00-ddl.sql -f /tmp/01-top-teams.sql
 3
 4SET 'pipeline.name' = 'game-top-teams';
 5SET 'parallelism.default' = '1';
 6-- Checkpoint every 10 seconds. The JDBC sink flushes its buffered rows at each
 7-- checkpoint, and after a restart the job resumes from the last checkpoint instead of
 8-- rebuilding its state from the start of the topic. odctl sets the checkpoint folder
 9-- and RocksDB state, but no interval, so the job sets it.
10SET 'execution.checkpointing.interval' = '10s';
11-- Keep a running total for an hour after its last score. A session lasts about 5
12-- minutes and a late score arrives about 7 minutes after it was earned, so no total is
13-- dropped while it can still change.
14SET 'table.exec.state.ttl' = '60 min';
15-- Collect updates for up to a second before writing, so a busy leaderboard is written
16-- once a second rather than once per event.
17SET 'table.exec.mini-batch.enabled' = 'true';
18SET 'table.exec.mini-batch.allow-latency' = '1s';
19SET 'table.exec.mini-batch.size' = '2000';
20
21-- Top teams: the 10 teams with the highest total score.
22INSERT INTO top_teams
23SELECT rnk, team_id, team_name, total_score
24FROM (
25  SELECT *, ROW_NUMBER() OVER (ORDER BY total_score DESC, team_name) AS rnk
26  FROM (
27    SELECT team_id, MAX(team_name) AS team_name, CAST(SUM(score) AS BIGINT) AS total_score
28    FROM scores
29    GROUP BY team_id
30  )
31)
32WHERE rnk <= 10;

The settings each job makes:

  • Checkpoints. A checkpoint is a saved copy of a job’s state. Each job checkpoints every 10 seconds. The JDBC sink writes its buffered rows at each checkpoint, and after a restart a job resumes from its last checkpoint.
  • State time to live. Flink keeps a running total for each team or player in its state. table.exec.state.ttl drops a total that has not changed for the time set. The totals are kept for an hour, which is longer than a session and a late score together.
  • Mini-batches. Each job collects updates for up to a second before writing (table.exec.mini-batch.*), so a busy leaderboard is written once a second rather than once per event.
  • Ranking. Each query ranks with ROW_NUMBER() OVER (ORDER BY ...) and keeps ranks 1 to 10.

The hot streaks job differs in its settings. Its windows look back only 60 seconds, so it keeps a player’s rows for 5 minutes. Its mini-batches are half the size. These are the settings in leaderboard/jobs/03-hot-streaks.sql:

 1SET 'pipeline.name' = 'game-hot-streaks';
 2SET 'parallelism.default' = '1';
 3-- Checkpoint every 10 seconds. The JDBC sink flushes its buffered rows at each
 4-- checkpoint, and after a restart the job resumes from the last checkpoint instead of
 5-- rebuilding its state from the start of the topic. odctl sets the checkpoint folder
 6-- and RocksDB state, but no interval, so the job sets it.
 7SET 'execution.checkpointing.interval' = '10s';
 8-- The windows look back 60 seconds, so a player's rows are dropped 5 minutes after
 9-- their last score.
10SET 'table.exec.state.ttl' = '5 min';
11-- Collect updates for up to a second before writing, so a busy leaderboard is written
12-- once a second rather than once per event.
13SET 'table.exec.mini-batch.enabled' = 'true';
14SET 'table.exec.mini-batch.allow-latency' = '1s';
15SET 'table.exec.mini-batch.size' = '1000';

Its query averages each player’s scores over two windows, the last 10 seconds and the last 60 seconds, measured in event time. So late events, minutes behind, count towards the totals in the other three leaderboards, but the hot streaks leave them out.

To see a leaderboard directly in PostgreSQL:

1docker exec postgres psql -U user -d odctl -c "SELECT * FROM game.top_teams ORDER BY rnk"

Step 3: Open the Dashboard

1python -m leaderboard.app.ui
NiceGUI ready to go on http://127.0.0.1:8091

Open http://127.0.0.1:8091. The four charts update every 2 seconds while the simulation runs.

NiceGUI dashboard with bar charts of the top teams, top players, hot streaks and team MVPs
The leaderboard dashboard

Step 4: Change the Game While It Runs

The simulation reads parameter changes from the topic game-control while it runs, through desflow’s Kafka ingress. For example, make every new player a robot:

1python -m leaderboard.simulation.control game.variables.robot_share 1.0
Sent game.variables.robot_share = 1.0 to game-control

Robots play a round about every 1.5 seconds, four times as often as people. So within a few minutes, as the new players’ sessions go on, robots (RBT-...) take over the top players. Before the change, the top players were mostly people:

 rnk |  user_id   |    team_name     | total_score 
-----+------------+------------------+-------------
   1 | RBT-000005 | Ruby-Wombat      |         603
   2 | RBT-000016 | Coral-Kookaburra |         342
   3 | RBT-000013 | Coral-Kookaburra |         321
   4 | USR-000002 | Ruby-Wombat      |         151
   5 | USR-000011 | Ruby-Wombat      |         136
   6 | USR-000007 | Ruby-Wombat      |         135
   7 | USR-000001 | Ruby-Wombat      |         133
   8 | USR-000008 | Ruby-Wombat      |         120
   9 | USR-000015 | Ruby-Wombat      |         119
  10 | USR-000014 | Coral-Kookaburra |         116
(10 rows)

After the change, every one of the top players was a robot:

 rnk |  user_id   |    team_name     | total_score 
-----+------------+------------------+-------------
   1 | RBT-000013 | Coral-Kookaburra |        1802
   2 | RBT-000016 | Coral-Kookaburra |        1645
   3 | RBT-000035 | Coral-Kookaburra |        1414
   4 | RBT-000039 | Coral-Kookaburra |        1386
   5 | RBT-000037 | Olive-Platypus   |        1351
   6 | RBT-000041 | Olive-Platypus   |        1271
   7 | RBT-000047 | Jade-Platypus    |        1263
   8 | RBT-000005 | Ruby-Wombat      |        1260
   9 | RBT-000044 | Olive-Platypus   |        1250
  10 | RBT-000046 | Jade-Platypus    |        1245
(10 rows)

The team MVPs changed the same way. All 10 of the top MVPs were robots:

 rnk |  user_id   |    team_name     | player_total | team_total |    contrib_ratio    
-----+------------+------------------+--------------+------------+---------------------
   1 | RBT-000108 | Jade-Numbat      |          235 |        475 | 0.49473684210526314
   2 | RBT-000106 | Teal-Quokka      |          297 |        613 | 0.48450244698205547
   3 | RBT-000083 | Indigo-Dingo     |          671 |       3367 |  0.1992871992871993
   4 | RBT-000086 | Olive-Numbat     |          674 |       4302 | 0.15667131566713158
   5 | RBT-000005 | Ruby-Wombat      |         1260 |       9392 | 0.13415672913117546
   6 | RBT-000013 | Coral-Kookaburra |         1820 |      13839 | 0.13151239251390998
   7 | RBT-000037 | Olive-Platypus   |         1351 |      12947 | 0.10434849772147987
   8 | RBT-000047 | Jade-Platypus    |         1279 |      12470 | 0.10256615878107458
   9 | RBT-000060 | Olive-Emu        |         1058 |      10621 | 0.09961397231899068
  10 | RBT-000068 | Jade-Kookaburra  |          900 |       9474 | 0.09499683343888538
(10 rows)

The hot streaks stay with people while any are still playing. A streak divides a player’s average over the last 10 seconds by their average over the last minute. A robot plays 6 or 7 rounds in 10 seconds, so its short average stays close to its long one, and its ratio stays near 1. A person plays one or two rounds in 10 seconds, so one high score can double the short average. Once the last people’s sessions end, about 6 minutes after the change, only robots are left, and they fill the hot streaks too.

Set the share back with python -m leaderboard.simulation.control game.variables.robot_share 0.05. Other parameters work the same way. For example, game.arrival.player.rate 2.0 brings four times as many players, and so four times as many scores. python -m leaderboard.simulation.control --help lists them all.

Clean Up

Stop the simulation and the dashboard first, then:

1python -m leaderboard.stores.cleanup

It cancels the four Flink jobs, deletes the three Kafka topics and the schema subject, and drops the PostgreSQL schema game with its tables. It leaves other projects’ jobs, topics and tables alone, and the services keep running. It is safe to run again. To start over, run the steps again from Step 1.

To stop the services and delete their data:

1odctl down --all --volumes          # answer y; --volumes also deletes the data
2deactivate
3rm -rf .venv