A Topic (message subject) is a collection of messages of the same type. It serves as the basic unit for data writing operations in Message Queue for Kafka. This topic describes the steps to create a single Topic.
In real business scenarios, a Topic often carries the same type of business traffic. Developers decide how to design different Topics based on their system design and data architecture. Each Topic can receive messages sent by multiple producers. It can also allow multiple consumers to consume messages.
Partition is the physical grouping of a Topic. Each Topic can be divided into multiple partitions. Each partition is an ordered queue. When creating a Topic, you must specify the number of partitions. As business traffic grows, you can increase the number of partitions anytime to expand the Topic's capacity.
Message Queue for Kafka controls Topic creation methods supported by Kafka instances through the automatic Topic creation function. By default, this function is disabled.
- When disabled, Topics can only be created via the console.
- When enabled, you can create Topics through the console or directly specify data to write to a non-existent Topic when generating messages. In this case, the Kafka backend service automatically creates the Topic for you.
tip
Each specification instance provides a certain range of partition numbers and a number of free partitions. If the free partition quota is used up, you need to purchase partitions to continue creating or expanding Topics. If the number of partitions reaches the maximum limit of the specification, you must adjust the partition specification to obtain a larger partition quota before continuing to create or expand Topics.
Prerequisites
A Message Queue for Kafka instance is created, and the instance status is Running. For operation steps, please refer to Create instance. Create Topic manually
Message Queue for Kafka supports manually creating Topics one by one through the console or OpenAPI.
tip
- Once the Topic is created, the Topic name can not be changed. The number of partitions can only be increased, not reduced. Please plan the number of partitions reasonably when creating or modifying.
- In scenarios such as cloud migration or instance backup, if multiple Topics need to be created at once, you can batch create Topics by uploading a configuration file. For the operation steps, refer to Batch import Topic.
Create Topic through the console
- Select the region where the instance is located in the top menu bar.
- On the Instances page, click the specified instance name.
- Click the Topic management tab.
- Complete the basic configuration for the Topic.
- Complete the parameter configuration for the Topic.
- Configure user permissions.
- Assign SASL user permissions for the Topic within the specified instance.
- By default, the user permission is set to All allowed. That is, all SASL users have permissions to read and write this Topic, even if the user has disabled All Permitted permission. You can also choose custom settings to assign different permission types for different SASL users. Custom permissions include:
- Read only: has permission to subscribe to messages but not to produce messages.
- Write only: has permission to produce messages but not to subscribe to messages.
- Read and write: has both permissions to produce and subscribe to messages.
tip
- Custom settings only take effect on SASL users who have disabled All Permitted.
- When creating a Topic, if custom permissions are set, Message Queue for Kafka will simultaneously create corresponding ACLs and display them in the ACL list. If you need to modify custom permissions after creating a Topic, we recommend that you proceed through ACL operations. For details, please refer to Create ACL.
- Configure message cleanup policy.
- DELETE: the default message cleanup policy. When disk space is sufficient, messages within the maximum retention period are retained. When disk space is insufficient, previous messages will be deleted in advance to ensure service availability.
tip
The default disk cleanup threshold for Kafka instances is 90%. The actual threshold is based on your actual use. For details, see Set disk cleanup threshold. - COMPACT: The COMPACT message cleanup policy integrates messages according to their Keys. For messages with the same Key, only the latest value is retained, and previous records are removed.
warning
If the message does not have Key, COMPACT message cleanup policy is not supported.
- DELETE, COMPACT: both DELETE and COMPACT message cleanup policies are configured. Messages will be deleted once they meet any one of the cleanup policies.
- (optional) Configure topic tags.
- Click Add tag, enter the tag key and value to add custom tags to the topic.
- Tags identify and classify cloud resources. Adding tags helps recognize and manage topics. You can add up to 20 tags each time. For tag setting rules, refer to Tag setting rules.
- You can view created topics on the Topic management tab.
Create topics through OpenAPI
Message Queue for Kafka provides the OpenAPI CreateTopic for manually creating topics. Automatically create Topics
Before using the auto-create topic function, enable it in the console and specify a non-existent topic when writing messages via the client SDK. The default configuration for auto-created topics is as follows.
tip
- We recommend using the auto-create topic function only in testing or data migration scenarios. In actual production environments, especially for core production businesses with high performance and stability requirements, it is recommended to disable this function.
- Compared to manually creating a topic, automatically creating a topic takes more time. Besides, it only appears in the topic list two minutes after successful creation.
- During the instance configuration process, creating topics by any means is prohibited. Before creating a topic through the client, confirm the instance status is Running.
1 Enable automatic topic creation
- Select the region where the instance is located in the top menu bar.
- On the Instances page, click the specified instance name.
- In the Topic management tab, click Set up automatic topic creation.
- Enable the Automatically create Topics and set the following parameters before you click Create.
- In the prompt window, click OK.
tip
For instance created on or before October 26, 2023, the backend service will automatically restart the instance to apply the configuration when setting the default number of partitions or replicas.
2 Automatically create Topic through Kafka client SDK
After enabling the automatic Topic creation function, you can set a non-existent Topic name in the producer-side client SDK and request to write message data to it. Message Queue for Kafka will automatically create this Topic in the corresponding instance upon receiving the request.
You can refer to the following Java example code to set the message writing Topic for the producer instance in the Kafka client SDK. You can also refer to the example project provided by Message Queue for Kafka to develop related business logic. private void setTopic(Properties kafkaProperties) {
topic = kafkaProperties.getProperty("topic");
Related actions
After creating a Topic, you can modify its configuration and user permissions anytime as needed. You can also view detailed information about the Topic.
- Modify Topic configuration: Supports modifying the number of partitions, replicas, minimum in-sync replicas, message retention period, and maximum message size. For details, see Modify Topic configuration.
- Manage Topic tags: Supports filtering Topics by tags and batch binding or unbinding tags. For details, see Manage Topic tags.
- Edit user permissions: set SASL user permissions for this Topic through ACL. For details, see Create ACL.
- View Topic and partition details: check the status, number of partitions, and other information of the Topic. For details, see View Topic details.
- Export Topic list: export the Topic list via the console, including Topic names, replica counts, and other settings. For details, see Export Topic list.
- Delete Topic: delete a Topic when it is no longer used to save resources. For details, see Delete Topic.