An In‑Depth Introduction to Sogou’s Workflow C++ Server Framework

This article presents a comprehensive overview of the open‑source Workflow framework by Sogou, highlighting its high‑performance asynchronous architecture, rich feature set, practical C++ code examples for building servers and clients, and detailed explanations of its task‑flow and timer implementations for backend developers.

Refining Core Development Skills
Refining Core Development Skills
Refining Core Development Skills
An In‑Depth Introduction to Sogou’s Workflow C++ Server Framework

Hello, I am "Program Cat". Recently I discovered an open‑source project called workflow that is ideal for C++ developers looking to advance their skills. The project’s repository is at https://github.com/sogou/workflow .

workflow is Sogou’s server engine used by almost all of its backend C++ services and dozens of other companies, handling over a hundred billion requests daily.

Key Characteristics

User experience is excellent: the API is concise, supports common protocols, and is easy to use.

High performance: extensive testing shows it outperforms mainstream server frameworks in network, disk I/O, and CPU usage.

High stability: widely adopted by Sogou and many other companies.

Cross‑platform support: Linux, macOS, Windows, Android, etc.

Productivity boost: developers work with only tasks and task‑flows, without dealing with thread pools, connection pools, or low‑level async notifications.

Innovative design: the source code demonstrates advanced C++ techniques such as inheritance and task composition.

What the Framework Can Do

The framework can quickly build servers; for example, an HTTP server can be created with just a few lines of code:

#include <stdio.h>
#include "workflow/WFHttpServer.h"

int main() {
    WFHttpServer server([](WFHttpTask *task) {
        task->get_resp()->append_output_body("Hello World!");
    });
    if (server.start(8888) == 0) {
        getchar(); // press "Enter" to end.
        server.stop();
    }
    return 0;
}

The framework also serves as a universal asynchronous client, supporting HTTP, Redis, MySQL, WebSocket, and Kafka. Below is an example of a MySQL client task:

int main(int argc, char *argv[]) {
    // ... initialization code ...
    WFMySQLTask *task = WFTaskFactory::create_mysql_task(url, RETRY_MAX, mysql_callback);
    task->get_req()->set_query("SHOW TABLES;");
    // ... other setup ...
    task->start();
    // ...
}

By abstracting all asynchronous resources, Workflow eliminates the need for manual thread or connection management, delivering non‑blocking I/O performance that improves overall server throughput.

It also enables easy construction of complex task‑flow graphs, including serial, parallel, and mixed DAG structures. For instance, a simple graph can be built with:

WFGraphNode a, b, c, d;
a->b;
a->c;
b->d;
c->d;

The framework’s architecture relies on a hierarchy of SubTask, SleepRequest, and SleepSession classes. A timer task is created via a factory method:

static WFTimerTask *create_timer_task(const std::string &timer_name,
                                      unsigned int microseconds,
                                      timer_callback_t callback);

The WFTimerTask class inherits from SleepRequest and implements start(), dismiss(), and the overridden done() callback. The start sequence involves:

User calls WFTimerTask::start(). start() invokes Workflow::start_series_work(). start_series_work() dispatches the task, which triggers SleepRequest::dispatch().

The dispatcher calls Communicator::sleep(), which uses timerfd to schedule the timeout.

When the timer expires, SleepSession::handle() is called, which in turn calls subtask_done(). subtask_done() invokes the overridden done() method, executing the user’s callback.

This layered design allows new task types (e.g., file I/O, network, database) to be added with minimal effort, showcasing the framework’s extensibility.

In summary, Workflow offers a clean API for building high‑performance asynchronous servers and clients, a powerful task‑flow engine, and a well‑structured codebase that serves as an excellent learning resource for C++ backend developers.

GitHub: https://github.com/sogou/workflow Gitee: https://gitee.com/sogou/workflow

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

NetworkAsynchronousserverframeworkC++
Refining Core Development Skills
Written by

Refining Core Development Skills

Fei has over 10 years of development experience at Tencent and Sogou. Through this account, he shares his deep insights on performance.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.